Commonly Utilized JavaScript Methods Part IIII - TypedArray, Map, and Set

 Welcome back to the series, this article is covering objects/structures - TypedArray, Map and Set.


What is a TypedArray?

It is a data structure that hosts specific elements types in the confines of an array. The various element types that are associated with TypedArrays are 9:


1. Int8Array utilized to store byte type elements/integers ranging

from -128-127.

2. Unit8Array utilized to store octet type elements/integers ranging 

from 0 to 255.

3. Unit8ClampedArray utilized to store octet type elements/integers ranging

from 0 to 255 but they differ from Unit8Array - due to it being clamped.

4. Int16Array utilized to store long type elements/integers ranging

from -32668-32767.

5.Unit16Array utilized to store unsigned short type elements/integers 

ranging from 0 to 65535.

6. Int32Array utilized to store long types as well with 32-bit signed integers

ranging from -2147483648-214786347.

7. Uint32Array utilized to store unsigned long types with 32-bit unsigned integers

ranging from 0-4294967295.

8. Float32Array utilized to store 32-bit floating point numbers, and unrestricted float

types ranging from 1.2x1.0(to the power of 38)-3.4x10(to the power of 38).

9. Float64Array utilize to store 64-bit floating point numbers, and unrestricted doubles

ranging from 5.0x10324 to 1.8x10308.


Methods associated with typedArray methods include:

copywithin() - copying a portion of an array to another location

inside the same array and return the size of the array.

entries() - utilized to obtain key-value pairs of each index in array.

every() - checks whether all the elements satisfy condition appended.

fill() - appends values to every element in array.

filter() - Forms a new array under conditions passed.

find() - obtain value of first element that passes conditioned appended.

for of loop.


Example of Typed Array with For Of Loop:


const iterateArray = new Uint8Array([0x00, 0xff]);
//for of loop:
for (const value of iterateArray) {
console.log(value);
}
//Results: 0, 255


What is Map?



Map is essentially an object that retains the key-value pairs of elements that can appended via .set(), retrieved via .get(), removal via .delete(), conditions via .has() and volume is determined via size(). Map is created via a constructor which results to the appending of key and values pairs for the programmers dispense.


Examples of Map:


const simpsonChars = new Map(); //set constructor

simpsonChars.set("child1", "Bart");
simpsonChars.set("child2", "Lisa");
simpsonChars.set("child3", "Maggie");
simpsonChars.set("parent1", "Homer");
simpsonChars.set("parent2", "Marge");

console.log(simpsonChars);
/*

Results:
Map(5) {
'child1' => 'Bart',
'child2' => 'Lisa',
'child3' => 'Maggie',
'parent1' => 'Homer',
'parent2' => 'Marge'
}

*/

//get a character:
console.log(simpsonChars.get("child1")); //Bart
//removal a character:
console.log(simpsonChars.delete("child3")); //true
//Check if character is deleted:
console.log(simpsonChars.has("child3")); //false
//display map again:
console.log(simpsonChars);
/*
Results:

Map(4) {
'child1' => 'Bart',
'child2' => 'Lisa',
'parent1' => 'Homer',
'parent2' => 'Marge'
}

*/


What is Set?


Set is an object which enables programmers to store  any values as well as primitive values - which is string,

number, boolean, undefined, symbol and null although primitive values can't altered. Set like Map is invoked via a constructor, and has methods to add, delete and check volume as well. You can also iterate through sets as displayed in for of loop.


Example of Creating A Set:


const exampleSet = new Set(); //set constructor

exampleSet.add(4);
exampleSet.add("Lifers");
console.log(exampleSet); //result: Set(2) { 4, 'Lifers' }


This concludes my article, next topic shall be about Recursive functions.


Comments

Popular Posts