JS-Array Common Methods

To find out what native methods an array has, console output, as shown in the figure:




length: An instance property of an array that returns or sets the number of elements in an array.


toString(): You can convert an array to a string and return the result.

toLocaleString(): Returns the localized representation of each element in the array.

let a = [1234,'hello',21];
a.length; //3
a.toString();  //'1234','hello','21'
a.toLocaleString();  //'1,234','hello','21'


join([seperator]): Returns a string that is converted from each element and stitched together using the specified seperator.

let a = [1234,'hello',21];
a.join('-'); //'1234-hello-21'


concat(): Split two or more arrays and return the result.This method does not change the existing array, but returns a new one.Each parameter can be a value or an array and can accept any number of parameters.Can be used as a copy array.

let a = [1234,'hello',21];
let b = a.concat(50,60,[1,2,3]); //[1234, "hello", 21, 50, 60, 1, 2, 3]


reverse(): Reverses the order of elements in an array, and changes the contents of an existing array.

let a = [1234,'hello',21];
let b = a.reverse();
a; //[21, "hello", 1234]
b; //[21, "hello", 1234]


sort(): Reorder array elements by Unicode code.

let a = [1234,'hello',21,'bar',6,789];
a; //[1234, 21, 6, 789, "bar", "hello"]


slice(start, [end]): Returns a subarray of an existing array, starting at the position of the subscript start and ending at the subscript end (excluding the end). If the parameter is negative, it means that the end parameter can be omitted from the end.Can be used as a copy array.

let a = [1234,'hello',21,'bar',6,789];
let b = a.slice(1,4); //["hello", 21, "bar"]
let c = a.slice(2); //[21, "bar", 6, 789]
let d = a.slice(-4,-2); //[21, "bar"]
a; //[1234, "hello", 21, "bar", 6, 789]


splice(start,count,e1,e2,...en): delete one part of the element from the array and add another part. start specifies the starting position of the add/delete, can take a negative value, count is the number to delete, 0 means not delete, e1, E2 means the newly added item in start, return the item that will be deleted, the existing array will change.

let a = [10,20,30,40,50];
let b = a.splice(2,2,110);
b; //[30,40]
a; //[10,20,110,50,]
let c = a.splice(2,0,[70,80,90]);
c; //[]
a; //[10,20,[70,80,90],110,50]


push(): Adds one or more elements to the end of an array and returns the new length of the array.

unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.

pop(): Deletes the last element from the array and returns its value.This method changes the length of the array.

shift(): Deletes the first element from the array and returns its value.This method changes the length of the array.

let a = [10,20,30,40,50];
let a1 = a.push(60);
a1; //6
a; //[10,20,30,40,50,60]

let b = [10,20,30,40,50];
let b1 = b.unshift(60);
b1; //6
b; //[60,10,20,30,40,50]

let c = [10,20,30,40,50];
let c1 = c.pop();
c1; //50
c; //[10,20,30,40]

let d = [10,20,30,40,50];
let d1 = d.shift();
d1; //10
d; //[20,30,40,50]


includes(): Used to determine if an array contains a specified value, returns true if it does, or false if it does not.

let array1 = [1, 2, 3];
array1.includes(2); //true


indexOf(value, [fromIndex]): Returns the first index in the array where a given element can be found, or -1 if it does not exist.FromIndex is optional and specifies to start the lookup at the subscript.

lastIndexOf(value, [fromIndex]): Returns the last index of the specified element in the array, or -1 if it does not exist.Look forward from the back of the array, starting at fromIndex.

let beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
beasts.indexOf('bison'); //1
beasts.indexOf('bison', 2); //4
beasts.indexOf('giraffe'); //-1

beasts.lastIndexOf('bison'); //4
beasts.lastIndexOf('bison', 2); //1
beasts.lastIndexOf('giraffe'); //-1


find(): Returns the value of the first element in the array that satisfies the provided test function.Otherwise, return undefined.

findIndex(): Returns the index of the first element in the array that satisfies the provided test function.Otherwise return -1.

let array1 = [5, 12, 8, 130, 44];
let val = array1.find(element => element > 10);
let index = array1.findIndex(element => element > 10);
val; //12
index; //1


forEach(): Performs the supplied function once for each element of the array.

filter(): Creates a new array containing all the elements of the test implemented by the provided function.

map(): Creates a new array, the result of which is returned after each element in the array calls a supplied function.

reduce(reducer[,initialValue]): Perform a reducer function (ascending execution) on each element in the array, summarizing its results as a single return value.The reducer function takes four parameters: Accumulator (acc), Current Value (cur), Current Index (idx), Source Array (src), and source array.The initialValue is the value of the first parameter when the callback function is called for the first time.If no initial value is provided, the first element in the array is used.

Accumulator is an accumulator, current value is the current value, index is the current index, array is the source array.

every(): Tests whether all elements in an array can pass the test of a specified function.It returns a Boolean value.

some(): Tests whether at least one element in the array passes the function test provided.It returns a value of type Boolean.


let array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element)); //'a'\n 'b'\n 'c' 
array1; //['a', 'b', 'c']

let words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
let result = words.filter(word => word.length > 6);
result; //["exuberant", "destruction", "present"]
words; //["spray", "limit", "elite", "exuberant", "destruction", "present"]

let array2 = [1, 4, 9, 16];
let map1 = array2.map(x => x * 2);
map1; //[2, 8, 18, 32]
array2; //[1, 4, 9, 16]

let array3 = [1, 2, 3, 4];
let reducer = (accumulator, currentValue) => accumulator + currentValue;
array3.reduce(reducer); //10
array3.reduce(reducer, 5); //15
array3; //1, 2, 3, 4]

let isBelowThreshold = (currentValue) => currentValue < 40;
let array4 = [1, 30, 39, 29, 10, 13];
let array5 = [1, 30, 39, 291, 10, 13];
array4.every(isBelowThreshold); // true
array5.every(isBelowThreshold); // false

let array = [1, 2, 3, 4, 5];
let even = (element) => element % 2 === 0;
array.some(even); // true


flat([depth]): The array is recursively traversed at a specified depth, and all elements are merged with the elements in the traversed subarray to be returned as a new array.Depth specifies the depth of the structure to extract the nested array, defaulting to 1.Can be used as a flattened nested array.

let arr1 = [1, 2, [3, 4],[5,[6,[7]]]];
let b = arr1.flat(); 
let c = arr1.flat(2); 
let d = arr1.flat(Infinity); 
arr1; // [1, 2, [3, 4],[5,[6,[7]]]]
b; // [1, 2, [3, 4],[5,[6,[7]]]]
c; //[1,2,3,4,5,[6,[7]]]
d; //[1,2,3,4,5,6,7]





Array.from(): Create a new, shallow copy of an array instance from an array-like or iterative object.

Array.of(): Used to convert a set of values into an array.

let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1


Array.isArray(): Determines if the value passed is an array.

Array.isArray([1, 2, 3]);  // true
Array.isArray({foo: 123}); // false
Array.isArray("foobar");   // false
Array.isArray(undefined);  // false


That's all. I'll add it when I'm free.


Keywords: Javascript

Added by ultranet on Fri, 03 Jan 2020 06:35:15 +0200