Skip to content

Latest commit

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

JavaScript Array Methods Cheat Sheet

constarr=[1,3,5,7];constarr2=[1,3,5,5,7];constarr3=['d','a','b','c'];constarr4=[1,30,2,100];constusers=[{name: 'grace',city: 'Toronto',age: 30},{name: 'monica',city: 'New York',age: 24},];
MethodCode ExampleResultReturn TypeIn Place
keys()const iterator = arr.keys();
for (const key of iterator) {
console.log(key);
}
0
1
2
3
Array IteratorNo
values()const iterator = arr.values();
for (const value of iterator) {
console.log(value);
}
1
3
5
7
Array IteratorNo
push()console.log(arr.push(9));
console.log(arr);
5 // new length of the array
[1, 3, 5, 7, 9]
numberNo
pop()console.log(arr.pop());
console.log(arr);
7 // last element of the array
[1, 3, 5]
elementNo
shift()console.log(arr.shift());
console.log(arr);
1 // removed element
[3, 5, 7]
elementNo
unshift()console.log(arr.unshift(2, 4));
console.log(arr);
6 // new length of the array
[2, 4, 1, 3, 5, 7]
numberNo
slice()console.log(arr.slice(1, 3));
console.log(arr);
// remain from index 1 to 2 (last one is not included)
[3, 5]
[ 1, 3, 5, 7 ]
arrayNo
splice()arr.splice(1, 0, 2); // insert ‘2’ at index 1
console.log(arr);
[1, 2, 3, 5, 7]arrayYes
splice()arr.splice(3, 1, 4); // replace 1 item at index 3
console.log(arr);
[1, 3, 5, 4]arrayYes
splice()console.log(arr.splice(1, 2)); // remove 2 items from index 1
console.log(arr);
[3, 5] // removed items
[1, 7] // array as a result
arrayYes
join()console.log(arr.join());1,3,5,7stringNo
indexOf()console.log(arr.indexOf(3));1numberNo
lastIndexOf()console.log(arr2.lastIndexOf(5));3numberNo
reduce()console.log(arr.reduce((pre, curr) => pre + curr));16numberNo
find()console.log(arr.find((num) => num > 3));5first oneNo
findIndex()console.log(arr.findIndex((num) => num > 3));2first oneNo
every()console.log(arr.every((num) => num % 2 === 1));TRUEbooleanNo
every()console.log(arr.every((num) => num > 10));FALSEbooleanNo
some()console.log(arr.some((num) => num > 5));TRUEbooleanNo
filter()console.log(arr.filter((num) => num > 3));
const newUsers = users.filter((user) => user.age > 25);
console.log(newUsers);
[5, 7]
[ { name: 'grace', city: 'Toronto', age: 30 } ]
arrayNo
map()arr.map((item) => item * 2)
users.map((user) => console.log(user.name));
[2, 6, 10, 14]
grace
monica
arrayNo
flatMap()console.log(arr.flatMap((x) => [x, x * 2]));[1, 2, 3, 6, 5, 10, 7, 14]arrayNo
forEach()arr.forEach((item) => console.log(item * 2));2
6
10
14
each elementNo
includes()console.log(arr.includes(3));TRUEbooleanNo
isArray()console.log(Array.isArray([1, 2, 3]));TRUEbooleanNo
reverse()console.log(arr.reverse());[7, 5, 3, 1]arrayYes
concat()const newArr = arr.concat(['a', 'b']);
console.log(newArr);
[1, 3, 5, 7, "a", "b"]arrayNo
fill()console.log(arr.fill('a', 1, 3));
// fill with 'a' from position 1 until position 3
[1, "a", "a", 7]arrayNo
flat()const newArr = [1, 3, 5, 7, [2, 4]];
console.log(newArr.flat());
[1, 3, 5, 7, 2, 4]arrayNo
Array.from()console.log(Array.from([1, 2, 3], (x) => x * 2));[2, 4, 6]arrayNo
Array.from()console.log(Array.from('grace'));["g", "r", "a", "c", "e"]arrayNo
sort()console.log(arr3.sort());["a", "b", "c", "d"]arrayYes
sort()console.log(arr4.sort());
console.log(arr4.sort((a, b) => a - b));
[ 1, 100, 2, 30 ]
[1, 2, 30, 100]
arrayYes

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors