The Array.of() method creates a new Array instance with any number of arguments,regardless of their type.
ex :
let monthlySales = Array.of(12,9,3);
let monthlyLabels = Array.of('Oct','Nov','Dec');
let deptSales = Array.of(12,9,3);
let deptLabels = Array.of('Hiking','Running','Hunting');
Using the spread Operator with array
ex :
let monthlySales = Array.of(12,9,3);
let yearlyTotal= addYearlyTotal(...monthlySales);
function addYearlyTotal(a,b,c){
return a+b+c;
}
ex :
let octNums = Array.of(1200,1000,9000);
let novNums = Array.of(1100,2000,9000);
let decNums = Array.of(4000,1000,5000);
let total = Array.of(...octNums,...novNums,...decNums);
Using Array.find and Array.findIndex to find a value
ES6 Array.find() and Array.findIndex() methods which provide easy ways to search for an element in Javascript arrays.
Array.find() method returns the value of the first element in an array that passes a given test.
Note :
1.Test must be provided as a function.
2.find() method executes a callback function once for each element in the array until it finds a value that returns true.
3.If nothing passes, undefined is returned.
4.find() does not mutate or change the original Array.
ex :
let monthlySales=Array.of(500,9000,3000)
let firstThousand = monthlySales.find(element => element > 1000);
console.log(firstThousand); // 9000
findIndex() returns the index of the first element in the array that satisfies the given test.
ex :
let monthlySales=Array.of(500,9000,3000);
let firstThousand = monthlySales.findIndex(element => element > 1000);
console.log(firstThousand); // 2
Array.fill() :
you want to take an array and then just put all the numbers back to zero.
ex :
let monthlySales=Array.of(500,9000,3000);
monthlySales.fill(0);
console.log(monthlySales); // [0, 0, 0]
Methods for Iterating through Arrays :
Array.forEach() is an Array method that we can use to execute a function on each element in an array.
it can only be used on Arrays,Maps and Sets.
ex :
const arr = ['cat', 'dog', 'fish'];
arr.forEach(element => {
console.log(element);
});
// cat
// dog
// fish
ex :
let monthlySales=Array.of(500,9000,3000);
let yearlyTotal =0;
function addYearlyTotal(x){
yearlyTotal = x + yearlyTotal;
}
monthlySales.forEach(addYearlyTotal);
console.log(yearlyTotal); // 12500
When using forEach, we simply have to specify a callback function.
This callback will be executed on each element in the array.
sets && WeakSets :
Sets enables you to store unique values of any type, whether primitive values or object references.
you can determine the size of the set by using the Size property.
ex :
const set1 = new Set();
set1.add(42);
set1.add('forty two');
set1.add('forty two');
console.log(set1.size); // 2
Set Methods :
Add,clear,Delete,Entries,forEach,Has,Keys,Values
ex : add
const set1 = new Set();
set1.add('3000');
const test = new Set();
test.add(5).add(4);
const set2 = new Set(['5000','2000']);
ex: delete
const monthlySales = new Set();
monthlySales.add('5000');
monthlySales.delete('5000');
Iterating a set :
ex :
const monthlySales = new Set();
monthlySales.add('5000');
for(let total of monthlySales)
console.log(total); // 5000
monthlySales.forEach(element => {
console.log( element); // 5000
});
Note : Array.from enables creating a new shallow-copied array from an array-like object
or an iterable(Array,String,Map,Set) object.
ex :
const cars = new Set(['Porsche', 'Ferrari']);
const carsCopy = Array.from(cars);
console.log(carsCopy); // ["Porsche", "Ferrari"]
Set vs WeakSet :
WeakSet :
1.Only contains objects.
2.No primitive data types.
3.Objects are held 'weakly'.
4.Not iterable
5.No access to size property
6.Garbage collected
Weakset Methods : Add,Delete and Has
ex :
const categories = new WeakSet();
categories.add({category:'Hiking'});
let running= { category:'Running'};
categories.add(running);
console.log(categories.has(running)); // true
Using Maps in Javascript:
Map uses key-value pairs and keeps the original insertion order of the keys.
Any value (objects and primitive values) may be used as either a key or a value.
Map Methods :
Set,delete,clear,get,entries,forEach,has,keys,values
ex:
const monthlySales = new Map();
monthlySales.set('newsale',5000);
console.log(monthlySales); // {"newsale" => 5000}
console.log(monthlySales.get('newsale')); // 5000
console.log(monthlySales.has('newsale')); // true
monthlySales.delete('newsale');
Iterating through a Map :
ex:
const monthlySales = new Map();
monthlySales.set('Oct',5000);
let arraydata= Array.from(monthlySales.keys());
console.log(arraydata); // ["Oct"]
monthlySales.forEach( function(sale){
console.log(sale); // 5000
});
for(let amount of monthlySales.values()){
console.log(amount); // 5000
}
Weakmap :
1.Keys must be objects.
2.Object are held "weakly".
3.Not iteratable
4.Garbage collected
5.WeakMaps are not enumerable
WeakMap Methods : Set,get,has and delete
ex :
const monthlySales = new WeakMap();
let salesA= { a:[1,2]};
monthlySales.set(salesA,'Hiking');
console.log(monthlySales); // {{...} => "Hiking"}
No comments:
Post a Comment