Saturday, March 4, 2017

ES6 Array: From, of, find and findIndex

Array.from - turns array-ish things into arrays
Array.of - creates arrays from arguments
These 2 are not on the prototype, they are a part of the Array class itself.

Array.from might be useful if we need to use something like a map function on a nodelist
We know that document.querySelectorAll(); returns a nodelist and not an array.
Thus, it is array-ish.

Array.from takes in a second argument which gives us the ability to map over that array
const peopleArray = Array.from(people, person=>person.textContent);

Another case would be if we have a function like 
sumAll(1,2,3,4,5,5,63,2,4,39);
We can handle its arguments like 
function sumAll(){
const nums=Array.from(arguments);
}
and then perform a reduce on it like
const total= nums.reduce((prev,next)=>{return prev+next;},0)

posts.find(post=> post.code===code);//returns the entry itself
posts.findIndex(post=> post.code===code);//returns the array's index # of the first entry that satisfies the condition


const doSomeSatisfy = ages.some(age=>age>10);
const doAllSatisty = ages.every(age=>age>10);




No comments:

Post a Comment