Writing .map() & .every () from scratch

Screen Shot 2017-03-15 at 11.36.10 AM

Screen Shot 2017-03-15 at 11.36.58 AM

Advertisements
Writing .map() & .every () from scratch

Some Array Methods

var newArray= Array.from(oldArray);

var newArray = oldArray.slice(); 

Makes a new copy of the old array instead of just referencing it.


Array.isArray(input); 

Checks if a value is an array.


arr.slice(begin, end);

Extracts a portion of an array based on provided indices. (Omitting begin will default it to index 0.  Omitting end will default to extracting up to the end arr.length).


arr.splice(startIndex, deleteCount, ‘item1‘, item2‘, etc.);

Remove or insert element where start is.  (Set deleteCount to 0 if you’re inserting).

Some Array Methods

Array.prototype.forEach()

.forEach() executes the callback function on each item of the array once.

arr.forEach(function callback(currentValue, index, array) {
    //your iterator
}[, thisArg]);

How to use:

  1. Create a callback function
    function logArrayElements(element, index, array) {
      console.log('a[' + index + '] = ' + element);
    }
  2. Apply callback function to .forEach() method
    [2, 5, , 9].forEach(logArrayElements);
    
    // a[0] = 2
    // a[1] = 5
    // a[3] = 9

* This can be combined into one step if the callback function is simple.

 

Array.prototype.forEach()