JavaScript Array methods

.filter()

creates new array with all elements that pass the test function

returns new Array
const srcArr = [1, 2, 3, 4, 5, 6, 7, 8];
let tempArr = [];

for (let i = 0; i < srcArr.length; i++) {
    if(srcArr[i] > 5) {
        tempArr.push(srcArr[i]);
    }
}

console.log(tempArr); // [6, 7, 8]
const srcArr = [1, 2, 3, 4, 5, 6, 7, 8];
const tempArr = srcArr.filter(val => val > 5);

console.log(tempArr); // [6, 7, 8]

const tempArr2 = srcArr.filter((val) => {
    return val > 5;
}); 

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

.map()
returns new Array
const srcArr = [1, 2, 3, 4];
let tempArr = [];

for (let i = 0; i < srcArr.length; i++) {
    tempArr.push(srcArr[i] * 2);
}

console.log(tempArr); // [2, 4, 6, 8]
const srcArr = [1, 2, 3, 4];
const tempArr = srcArr.map(val => val * 2);

console.log(tempArr); // [2, 4, 6, 8]

const tempArr2 = srcArr.map((val) => {
    return val * 2;
}); 

sorts the elements of an array in place

.sort()
mutates same Array
let numbers = [4, 2, 5, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers); // [1, 2, 3, 4, 5]

let heroes = [
  { name: 'Ironman', age: 33 },
  { name: 'Thor', age: 1589 },
  { name: 'Captain America', age: 154 },
  { name: 'The Vision', age: 0 },
  { name: 'The Scarlet Witch', age: 19 }
];

heroes.sort((a, b) => a.age > b.age ? -1 : 1);

console.log(heroes);

/**
[
  { name: 'Thor', age: 1589 },
  { name: 'Captain America', age: 154 },
  { name: 'Ironman', age: 33 },
  { name: 'The Scarlet Witch', age: 19 },
  { name: 'The Vision', age: 0 }
]
**/

applies a function against an accumulator and each value of the array
(from left-to-right)

.reduce()
returns accumulator
const numbers = [4, 2, 5, 1, 3];
const total = numbers.reduce((total, val) => { 
    return total + val; 
}, 0);
console.log(total); // 15

let items = [
  { desc: 'iPhone 6', quantity: 105 },
  { desc: 'iPhone 7', quantity: 220 },
  { desc: 'Macbook Pro 2016', quantity: 56 },
  { desc: 'iPad', quantity: 88 },
  { desc: 'iPad Mini', quantity: 97 }
];

const totalInventory = items.reduce((total, {quantity}) => { 
    return total + quantity; 
}, 0);
console.log(totalInventory); // 566

tests whether at least one element in the array passes the provided test function

.some()
returns Boolean
const numbers = [4, 2, 5, 1, 3];
numbers.some((elem, i, arr) => element % 2 == 0); // true
numbers.some((elem, i, arr) => element % 2 == 1); // true
numbers.some((elem, i, arr) => element > 10); // false

tests whether all elements in source array pass the provided test function

.every()
returns Boolean
const numbers = [4, 2, 5, 1, 3];
numbers.every((elem, i, arr) => element % 2 === 0); // false
numbers.every((elem, i, arr) => element % 2 === 1); // false
numbers.every((elem, i, arr) => element < 10); // true

returns first value in the array that satisfies the provided testing function

.find()
returns array element
const numbers = [1, 3, 5, 7, 8, 10, 20];
const firstEven = numbers.find(elem => elem % 2 === 0);
console.log(firstEven); // 8

returns index of the first element that satisfies the testing function

.findIndex()
returns Number (index)

Great for working with objects!

const numbers = [1, 3, 5, 7, 8, 10, 20];
const firstEven = numbers.findIndex(elem => elem % 2 === 0);
console.log(firstEven); // 4


let doctors = [
  { name: 'Dr. Chang', isOnCall: false },
  { name: 'Dr. Smith', isOnCall: false },
  { name: 'Dr. Brown', isOnCall: true },
  { name: 'Dr. Lopez', isOnCall: false },
  { name: 'Dr. Waterson', isOnCall: true }
];
const onCallIndex = doctors.findIndex(({isOnCall}) => !!isOnCall);

/** Same as: 
const firstOnCall = doctors.findIndex(doctor => {
    return doctor.isOnCall === true;
});
**/

system.page(doctors[onCallIndex].name); 

Take aways

  • ES2015 (ES6) features

  • Better readability than for-loops

  • They are still for-loops internally

    • ​careful with unintentional nesting

  • ​Do not all return same values

    • new array vs. value vs. boolean

JS Array Methods

By Carlos Filoteo

JS Array Methods

  • 721