Useful Javascript Array functions

Nikhil Nambiar
2 min readMay 23, 2020

--

#1: Array.map()

Applies something on every element and outputs a new array

let cars = [{ id: 1, name: "Mercedes"}, { id: 2, name: "Red Bull"}, { id: 3, name: "Ferrari"}, { id: 4, name: "Renault"}]cars.map( car => car.name )// ["Mercedes", "Red Bull", "Ferrari", "Renault"]cars.map( car => car.name + "sssss")// [1, 2, 3, 4]

#2: Array.filter()

Filters out all elements that match condition and outputs an array with filtered elements

cars.filter( car => car.name.length > 7)// [ {id: 1, name: "Mercedes"}, {id: 2, name: "Red Bull"} ]

#3: Array.find()

Returns the first match for the condition

// ["Mercedes", "Red Bull", "Ferrari", "Renault"]mappedCars.filter( car => car.length > 7 )//  ["Mercedes", "Red Bull"]mappedCars.find( car => car.length > 7 )// "Mercedes"

#4: Array.every()

Returns boolean after checking every element in the array matches condition

// ["Mercedes", "Red Bull", "Ferrari", "Renault"]mappedCars.every( car => car.endsWith("s") )// false

#5: Array.some()

Returns boolean after checking if one of the elements in the array matches condition

// ["Mercedes", "Red Bull", "Ferrari", "Renault"]mappedCars.some( car => car.endsWith("s") )// true

#6: Array.includes()

Returns boolean after checking if one of the elements in the array matches string

// ["Mercedes", "Red Bull", "Ferrari", "Renault"]mappedCars.includes("Red Bull")// true// case sensitivemappedCars.includes("red bull")// false// case insensitive mappedCars.toLowerCase().includes("red bull")
//
true

#7: Array.shift() and Array.shift()

// ["Mercedes", "Red Bull", "Ferrari", "Renault"]mappedCars.unshift("Williams")// ["Williams", "Mercedes", "Red Bull", "Ferrari", "Renault"]mappedCars.shift()// ["Mercedes", "Red Bull", "Ferrari", "Renault"]

--

--

Nikhil Nambiar
Nikhil Nambiar

No responses yet