28 Javascript Array Methods: A Cheat Sheet for Developer

Rahul Sharma (DevsMitra)
7 min readMar 28, 2022

Let’s understand javascript array functions and how to use them.

28 Javascript Array Hacks: A Cheat Sheet for Developer

Array.map()

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

const list = [😫, 😫, 😫, 😫];
list.map((⚪️) => 😀); // [😀, 😀, 😀, 😀]
// Code
const list = [1, 2, 3, 4];
list.map((el) => el * 2); // [2, 4, 6, 8]

Array.filter()

Returns a new array with all elements that pass the test implemented by the provided function.

const list = [😀, 😫, 😀, 😫];
list.filter((⚪️) => ⚪️ === 😀); // [😀, 😀]
// Code
const list = [1, 2, 3, 4];
list.filter((el) => el % 2 === 0); // [2, 4]

Array.reduce()

Reduce the array to a single value. The value returned by the function is stored in an accumulator (result/total).

const list = [😀, 😫, 😀, 😫, 🤪];
list.reduce((⬜️, ⚪️) => ⬜️ + ⚪️); // 😀 + 😫 + 😀 + 😫 + 🤪
// OR
const list = [1, 2, 3, 4, 5];
list.reduce((total, item) => total + item, 0); // 15

Array.reduceRight()

--

--

Rahul Sharma (DevsMitra)
Rahul Sharma (DevsMitra)

Written by Rahul Sharma (DevsMitra)

I’m a technology enthusiast who does web development. Passionate to contribute to open-source projects and make cool products.

No responses yet