Prototype Methods – Iteration

Returning Iterable: Returning Iterable: Returning Iterable:

Array.prototype.keys()

Returns a new Array Iterator that contains the keys for each index in the array.

Array.prototype.values()

( N ot supported by browsers a s of July 2017 .)Returns a new Array Iterator object that contains the values for each index in the array.

Array.prototype.entries()

Returns a new Array Iterator object that contains the key/value pairs for each index in the array.


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var a = ['a','b','c'];
      var e = a.entries();
   console.log(e.next().value); // [0,'a']
   console.log(e.next().value); // [1,'b']
   console.log(e.next().value); // [2,'c']
      var k = a.keys();
   console.log(k.next().value); // 0
   console.log(k.next().value); // 1
   console.log(k.next().value); // 2
      var v = a.values();
   console.log(v.next().value); // a
   console.log(v.next().value); // b
   console.log(v.next().value); // c
     console.log(v.next().value); // undefied
   

</script></body><html>

Taking Tester Function: Taking Tester Function: Taking Tester Function:

Array.prototype.some()

Returns true if at least one element in this array satisfies the provided testing function.

Array.prototype.every()

Returns true if every element in this array satisfies the provided testing function.

Array.prototype.find()

Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found.

Array.prototype.findIndex()

Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found.

Array.prototype.filter()

Creates a new array with all of the elements of this array for which the provided filtering function returns true.


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var a = [3,5,9,1,3,2,4,7,8];
   function biggerThan5(n){return n>5;}
   console.log(a.some(biggerThan5));  // true
   console.log(a.every(biggerThan5)); // false
   console.log(a.find(biggerThan5)); // 9
   console.log(a.findIndex(biggerThan5)); // 2
   console.log(a.filter(biggerThan5)); // [9,7,8]

</script></body><html>
An efficient way to remove empty elements.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

var a = [1,2,,3,,3,null,,0,,undefined,4,,4,,5,,6,,,,];a = a.filter(n => n);console.log(a); // [1, 2, 3, 3, 4, 4, 5, 6]

</script></body><html>

Returning a Single Value: Returning a Single Value: Returning a Single Value:

Array.prototype.reduce()

Apply a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

Array.prototype.reduceRight()

Apply a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var a = [1,2,3,4];
   function minus(a,b){return a-b;}
   console.log(a.reduce(minus));  // -8
   console.log(a.reduceRight(minus));  // -2

</script></body><html>

Others Others Others : : :

Array.prototype.map()

Creates a new array with the results of calling a provided function on every element in this array.

Array.prototype.forEach()

Calls a function for each element in the array. To allow 'loop' breaking, consider using .some() instead. The callback function takes one mandatory parameter (ie. the element in the array) followed by two optional parameters (ie. the index and the whole array). forEach() optionally takes a second parameter (ie. the value to be used as 'this').


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var a = [3,5,9,1,3,2,4,7,8];
   function square(n){return n*n;}console.log(a.map(square));  // [9,25,81,1,9,4,16,49,64]
   function printSquare(n){console.log(n*n);}a.forEach(printSquare);  // 9 25 81...

</script></body><html>

Array.prototype.flat ()

Flattens a nested array.

Array.prototype. flatM ap()

Equivalent to arr.map(f).flat().


RESETRUNFULL
<!DOCTYPE html><html><body><script>

const a1 = [1,[2,3,[4,5],6],7];console.log(a1.flat());          // [1,2,3,[4,5],6,7]console.log(a1.flat(Infinity));  // [1,2,3,4,5,6,7]const a2 = ['Who says leaning JavaScript is not fun?',
                  'Are you kidding me?'];console.log(a2.flatMap(e=>e.split(' ')));  //["Who", "says", "leaning", "JavaScript", "is", "not", "fun?", "Are", "you", "kidding", "me?"]

</script></body><html>