1

28 Javascript Array Methods: A Cheat Sheet for Developer

 2 years ago
source link: https://dev.to/devsmitra/28-javascript-array-hacks-a-cheat-sheet-for-developer-5769
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for 28 Javascript Array Methods: A Cheat Sheet for Developer
Rahul Sharma

Posted on Mar 28

โ€ข Updated on Apr 2

28 Javascript Array Methods: A Cheat Sheet for Developer

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

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]

Enter fullscreen mode

Exit fullscreen mode


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]

Enter fullscreen mode

Exit fullscreen mode


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

Enter fullscreen mode

Exit fullscreen mode


Array.reduceRight()

Executes a reducer function (that you provide) on each element of the array resulting in a single output value(from right to left).

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.reduceRight((โฌœ๏ธ, โšช๏ธ) => โฌœ๏ธ + โšช๏ธ); // ๐Ÿคช + ๐Ÿ˜ซ + ๐Ÿ˜€ + ๐Ÿ˜ซ + ๐Ÿ˜€

// Code
const list = [1, 2, 3, 4, 5];
list.reduceRight((total, item) => total + item, 0); // 15

Enter fullscreen mode

Exit fullscreen mode


Array.fill()

Fill the elements in an array with a static value.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.fill(๐Ÿ˜€); // [๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜€]

// Code
const list = [1, 2, 3, 4, 5];
list.fill(0); // [0, 0, 0, 0, 0]

Enter fullscreen mode

Exit fullscreen mode


Array.find()

Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.find((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜€); // ๐Ÿ˜€
list.find((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜); // undefined

// Code
const list = [1, 2, 3, 4, 5];
list.find((el) => el === 3); // 3
list.find((el) => el === 6); // undefined

Enter fullscreen mode

Exit fullscreen mode


Array.indexOf()

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.indexOf(๐Ÿ˜€); // 0
list.indexOf(๐Ÿ˜ก); // -1

// Code
const list = [1, 2, 3, 4, 5];
list.indexOf(3); // 2
list.indexOf(6); // -1

Enter fullscreen mode

Exit fullscreen mode


Array.lastIndexOf()

Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.lastIndexOf(๐Ÿ˜€); // 3
list.lastIndexOf(๐Ÿ˜€, 1); // 0

// Code
const list = [1, 2, 3, 4, 5];
list.lastIndexOf(3); // 2
list.lastIndexOf(3, 1); // -1

Enter fullscreen mode

Exit fullscreen mode


Array.findIndex()

Returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.findIndex((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜€); // 0

// You might be thinking how it's different from `indexOf` ๐Ÿค”
const array = [5, 12, 8, 130, 44];
array.findIndex((element) => element > 13); // 3

// OR
const array = [{
  id: ๐Ÿ˜€
}, {
  id: ๐Ÿ˜ซ
}, {
  id: ๐Ÿคช
}];

array.findIndex((element) => element.id === ๐Ÿคช); // 2

Enter fullscreen mode

Exit fullscreen mode


Array.includes()

Returns true if the given element is present in the array.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.includes(๐Ÿ˜€); // true

// Code
const list = [1, 2, 3, 4, 5];
list.includes(3); // true
list.includes(6); // false

Enter fullscreen mode

Exit fullscreen mode


Array.pop()

Removes the last element from an array and returns that element.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.pop(); // ๐Ÿคช
list; // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ]

// Code
const list = [1, 2, 3, 4, 5];
list.pop(); // 5
list; // [1, 2, 3, 4]

Enter fullscreen mode

Exit fullscreen mode


Array.push()

Appends new elements to the end of an array, and returns the new length.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.push(๐Ÿ˜ก); // 5
list; // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช, ๐Ÿ˜ก]

// Code
const list = [1, 2, 3, 4, 5];
list.push(6); // 6
list; // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode

Exit fullscreen mode


Array.shift()

Removes the first element from an array and returns that element.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.shift(); // ๐Ÿ˜€
list; // [๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = [1, 2, 3, 4, 5];
list.shift(); // 1
list; // [2, 3, 4, 5]

Enter fullscreen mode

Exit fullscreen mode


Array.unshift()

Adds new elements to the beginning of an array, and returns the new length.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.unshift(๐Ÿ˜ก); // 6
list; // [๐Ÿ˜ก, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = [1, 2, 3, 4, 5];
list.unshift(0); // 6
list; // [0, 1, 2, 3, 4, 5]

Enter fullscreen mode

Exit fullscreen mode


Array.splice()

Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.splice(1, 2); // [๐Ÿ˜€, ๐Ÿ˜ซ]
list; // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = [1, 2, 3, 4, 5];
list.splice(1, 2); // [2, 3]
list; // [1, 4, 5]

Enter fullscreen mode

Exit fullscreen mode


Array.slice()

Returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.slice(1, 3); // [๐Ÿ˜ซ, ๐Ÿ˜€]
list; // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = [1, 2, 3, 4, 5];
list.slice(1, 3); // [2, 3]
list; // [1, 2, 3, 4, 5]

Enter fullscreen mode

Exit fullscreen mode


Array.join()

Joins all elements of an array into a string.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.join('ใ€ฐ๏ธ'); // "๐Ÿ˜€ใ€ฐ๏ธ๐Ÿ˜ซใ€ฐ๏ธ๐Ÿ˜€ใ€ฐ๏ธ๐Ÿ˜ซใ€ฐ๏ธ๐Ÿคช"

// Code
const list = [1, 2, 3, 4, 5];
list.join(', '); // "1, 2, 3, 4, 5"

Enter fullscreen mode

Exit fullscreen mode


Array.reverse()

Reverses the order of the elements in an array.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.reverse(); // [๐Ÿคช, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€]
list; // [๐Ÿคช, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€]

// Code
const list = [1, 2, 3, 4, 5];
list.reverse(); // [5, 4, 3, 2, 1]
list; // [5, 4, 3, 2, 1]

Enter fullscreen mode

Exit fullscreen mode


Array.sort()

Sorts the elements of an array in place and returns the array. The default sort order is according to string Unicode code points.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.sort(); // [๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜ซ, ๐Ÿคช]

// This make more sense ๐Ÿค”
const array = ['D', 'B', 'A', 'C'];
array.sort(); // ๐Ÿ˜€ ['A', 'B', 'C', 'D']

// OR
const array = [4, 1, 3, 2, 10];
array.sort(); // ๐Ÿ˜ง [1, 10, 2, 3, 4]
array.sort((a, b) => a - b); // ๐Ÿ˜€ [1, 2, 3, 4, 10]

Enter fullscreen mode

Exit fullscreen mode


Array.some()

Returns true if at least one element in the array passes the test implemented by the provided function.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.some((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜€); // true
list.some((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜ก); // false

// Code
const list = [1, 2, 3, 4, 5];
list.some((el) => el === 3); // true
list.some((el) => el === 6); // false

Enter fullscreen mode

Exit fullscreen mode


Array.every()

Returns true if all elements in the array pass the test implemented by the provided function.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.every((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜€); // false

const list = [๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿ˜€];
list.every((โšช๏ธ) => โšช๏ธ === ๐Ÿ˜€); // true

// Code
const list = [1, 2, 3, 4, 5];
list.every((el) => el === 3); // false

const list = [2, 4, 6, 8, 10];
list.every((el) => el%2 === 0); // true

Enter fullscreen mode

Exit fullscreen mode


Array.from()

Creates a new array from an array-like or iterable object.

const list = ๐Ÿ˜€๐Ÿ˜ซ๐Ÿ˜€๐Ÿ˜ซ๐Ÿคช;
Array.from(list); // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

const set = new Set(['๐Ÿ˜€', '๐Ÿ˜ซ', '๐Ÿ˜€', '๐Ÿ˜ซ', '๐Ÿคช']);
Array.from(set); // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

const range = (n) => Array.from({ length: n }, (_, i) => i + 1);
console.log(range(10)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Enter fullscreen mode

Exit fullscreen mode


Array.of()

Creates a new array with a variable number of arguments, regardless of number or type of the arguments.

const list = Array.of(๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช);
list; // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = Array.of(1, 2, 3, 4, 5);
list; // [1, 2, 3, 4, 5]

Enter fullscreen mode

Exit fullscreen mode


Array.isArray()

Returns true if the given value is an array.

Array.isArray([๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]); // true
Array.isArray(๐Ÿคช); // false

// Code
Array.isArray([1, 2, 3, 4, 5]); // true
Array.isArray(5); // false

Enter fullscreen mode

Exit fullscreen mode


Array.at()

Returns a value at the specified index.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.at(1); // ๐Ÿ˜ซ

// Return from last ๐Ÿค”
list.at(-1); // ๐Ÿคช
list.at(-2); // ๐Ÿ˜ซ

// Code
const list = [1, 2, 3, 4, 5];
list.at(1); // 2
list.at(-1); // 5
list.at(-2); // 4

Enter fullscreen mode

Exit fullscreen mode


Array.copyWithin()

Copies array elements within the array. Returns the modified array.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.copyWithin(1, 3); // [๐Ÿ˜€, ๐Ÿ˜€, ๐Ÿคช, ๐Ÿ˜ซ, ๐Ÿคช]

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช];
list.copyWithin(0, 3, 4); // [๐Ÿ˜ซ, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = [1, 2, 3, 4, 5];
list.copyWithin(0, 3, 4); // [4, 2, 3, 4, 5]

Enter fullscreen mode

Exit fullscreen mode

NOTE: ๐Ÿค”

  • first argument is the target at which to start copying elements from.
  • second argument is the index at which to start copying elements from.
  • third argument is the index at which to stop copying elements from.

Array.flat()

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]];
list.flat(Infinity); // [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]

// Code
const list = [1, 2, [3, 4, [5, 6]]];
list.flat(Infinity); // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode

Exit fullscreen mode


Array.flatMap()

Returns a new array formed by applying a given callback function to each element of the array,

const list = [๐Ÿ˜€, ๐Ÿ˜ซ, [๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿคช]];
list.flatMap((โšช๏ธ) => [โšช๏ธ, โšช๏ธ + โšช๏ธ ]); // [๐Ÿ˜€, ๐Ÿ˜€๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜ซ๐Ÿ˜ซ, ๐Ÿ˜€, ๐Ÿ˜€๐Ÿ˜€, ๐Ÿ˜ซ, ๐Ÿ˜ซ๐Ÿ˜ซ, ๐Ÿคช, ๐Ÿคช๐Ÿคช]

// Code
const list = [1, 2, 3];
list.flatMap((el) => [el, el * el]); // [1, 1, 2, 4, 3, 9]

Enter fullscreen mode

Exit fullscreen mode


Must Read If you haven't

13 Typescript Utility: A Cheat Sheet for Developer

How to solve Express.js REST API routing problem with typescript decorators?

Javascript short reusable functions trick and tips


More content at Dev.to.

Catch me on Github, Twitter, LinkedIn, Medium, and Stackblitz.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK