0% found this document useful (0 votes)
14 views

ARRAY4

All JS Array methods

Uploaded by

Donald Andrew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

ARRAY4

All JS Array methods

Uploaded by

Donald Andrew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SN Method Description Code Example Output

Returns the element at the specified let arr = [10, 20, 30]; let
at(index) element = 30
index. Negative integers count back element = arr.at(-1);
1 from the last item in the array.
let arr1 = [1, 2]; let arr2 = [3,
concat(...items) Merges two or more arrays and 4]; let result = result = [1, 2, 3, 4]
2 returns a new array. arr1.concat(arr2);
let arr = [1, 2, 3]; let constructor =
constructor Refers to the function that created the
constructor = arr.constructor; Array
3 array's prototype, which is Array.
Copies a part of the array to another
copyWithin(target, let arr = [1, 2, 3, 4, 5];
location in the same array without arr = [4, 2, 3, 4, 5]
start, end) arr.copyWithin(0, 3, 4);
4 modifying its length.

Returns a new array iterator object let arr = ['a', 'b', 'c']; let iterator
[0, 'a'] [1, 'b'] [2,
entries() that contains the key/value pairs for = arr.entries(); for (let e of
'c']
iterator) console.log(e);
5 each index in the array.
Tests whether all elements in the array
every(callback, let arr = [1, 2, 3]; let result =
pass the test implemented by the result = true
thisArg) arr.every(x => x > 0);
6 provided function.
Fills all the elements of an array from a
let arr = [1, 2, 3, 4]; arr.fill(0, 1,
fill(value, start, end) start index to an end index with a arr = [1, 0, 0, 4]
3);
7 static value.

filter(callback, Creates a new array with all elements let arr = [1, 2, 3, 4]; let result =
result = [3, 4]
thisArg) that pass the test implemented by the arr.filter(x => x > 2);
8 provided function.
Returns the value of the first element
find(callback, let arr = [1, 2, 3, 4]; let result =
in the array that satisfies the provided result = 3
thisArg) arr.find(x => x > 2);
9 testing function.
Returns the index of the first element
findIndex(callback, let arr = [1, 2, 3, 4]; let index =
in the array that satisfies the provided index = 2
thisArg) arr.findIndex(x => x > 2);
10 testing function.
Returns the value of the last element
findLast(callback, let arr = [1, 2, 3, 4]; let result =
in the array that satisfies the provided result = 4
thisArg) arr.findLast(x => x > 2);
11 testing function.
Returns the index of the last element
findLastIndex(callba let arr = [1, 2, 3, 4]; let index =
in the array that satisfies the provided index = 3
ck, thisArg) arr.findLastIndex(x => x > 2);
12 testing function.

Creates a new array with all sub-array let arr = [1, [2, [3, [4]]]]; let result = [1, 2, 3,
flat(depth)
elements concatenated into it result = arr.flat(2); [4]]
13 recursively up to the specified depth.
Maps each element using a mapping
flatMap(callback, let arr = [1, 2, 3]; let result = result = [1, 2, 2, 4,
function, then flattens the result into a
thisArg) arr.flatMap(x => [x, x * 2]); 3, 6]
14 new array.
forEach(callback, Executes a provided function once for let arr = [1, 2, 3]; arr.forEach(x 2 4 6
thisArg) => console.log(x * 2));
15 each array element.
includes(valueToFin let arr = [1, 2, 3]; let result =
Determines whether an array includes result = true
d, fromIndex) arr.includes(2);
16 a certain value among its entries.

indexOf(searchElem let arr = [1, 2, 3]; let index =


Returns the first index at which a given index = 1
ent, fromIndex) arr.indexOf(2);
17 element can be found in the array.
Joins all elements of an array into a let arr = [1, 2, 3]; let str =
join(separator) str = '1-2-3'
18 string and returns this string. arr.join('-');
Returns a new array iterator object let arr = [1, 2, 3]; let iterator =
keys() that contains the keys for each index arr.keys(); for (let key of 012
19 in the array. iterator) console.log(key);

lastIndexOf(searchE let arr = [1, 2, 3, 2]; let index =


Returns the last index at which a given index = 3
lement, fromIndex) arr.lastIndexOf(2);
20 element can be found in the array.
Creates a new array with the results of
map(callback, let arr = [1, 2, 3]; let result =
calling a provided function on every result = [2, 4, 6]
thisArg) arr.map(x => x * 2);
21 element in the calling array.
Removes the last element from an let arr = [1, 2, 3]; let popped = popped = 3; arr =
pop()
22 array and returns that element. arr.pop(); [1, 2]
Adds one or more elements to the end
let arr = [1, 2]; let length = length = 4; arr = [1,
push(...items) of an array and returns the new length
arr.push(3, 4); 2, 3, 4]
23 of the array.

let arr = [1, 2, 3, 4]; let sum =


reduce(callback, Applies a function against an
arr.reduce((acc, val) => acc + sum = 10
initialValue) accumulator and each element in the
val, 0);
24 array to reduce it to a single value.
Applies a function against an
let arr = [1, 2, 3, 4]; let sum =
reduceRight(callbac accumulator and each element in the
arr.reduceRight((acc, val) => sum = 10
k, initialValue) array (from right to left) to reduce it to
acc + val, 0);
25 a single value.
Reverses the order of the elements in
reverse() let arr = [1, 2, 3]; arr.reverse(); arr = [3, 2, 1]
26 an array.
Removes the first element from an let arr = [1, 2, 3]; let shifted = shifted = 1; arr =
shift()
27 array and returns that element. arr.shift(); [2, 3]
Returns a shallow copy of a portion of let arr = [1, 2, 3, 4]; let result =
slice(start, end) result = [2, 3]
28 an array into a new array object. arr.slice(1, 3);

some(callback, Tests whether at least one element in let arr = [1, 2, 3]; let result =
result = true
thisArg) the array passes the test implemented arr.some(x => x > 2);
29 by the provided function.
sort(compareFuncti Sorts the elements of an array in place
let arr = [3, 1, 2]; arr.sort(); arr = [1, 2, 3]
30 on) and returns the sorted array.
Changes the contents of an array by
splice(start,
removing or replacing existing let arr = [1, 2, 3, 4];
deleteCount, arr = [1, 5, 6, 4]
elements and/or adding new elements arr.splice(1, 2, 5, 6);
...items)
31 in place.
Returns a string representing the str =
toLocaleString(local let arr = [1, 'a', new Date()]; let
elements of the array using their '1,a,7/16/2024,
es, options) str = arr.toLocaleString();
32 toLocaleString methods. 12:00:00 AM'
Similar to reverse(), but returns a new
let arr = [1, 2, 3]; let result =
toReversed() array with the elements in reversed result = [3, 2, 1]
arr.toReversed();
33 order.
toSorted(compareF Similar to sort(), but returns a new let arr = [3, 1, 2]; let result =
result = [1, 2, 3]
34 unction) array with the elements sorted. arr.toSorted();
toSpliced(start,
let arr = [1, 2, 3, 4]; let result =
deleteCount, Similar to splice(), but returns a new result = [1, 5, 6, 4]
arr.toSpliced(1, 2, 5, 6);
35 ...items) array with the specified modifications.
Returns a string representing the let arr = [1, 2, 3]; let str =
toString() str = '1,2,3'
36 specified array and its elements. arr.toString();
Adds one or more elements to the
let arr = [2, 3]; let length = length = 4; arr = [0,
unshift(...items) front of an array and returns the new
arr.unshift(0, 1); 1, 2, 3]
37 length of the array.

Returns a new array iterator object let arr = ['a', 'b', 'c']; let iterator
values() that contains the values for each index = arr.values(); for (let value of 'a' 'b' 'c'
iterator) console.log(value);
38 in the array.
Creates a new array with the specified
let arr = [1, 2, 3]; let result =
with(index, value) element replaced by a new value at a result = [1, 4, 3]
arr.with(1, 4);
39 given index.

You might also like