This is the 26th day of my participation in the August Text Challenge.More challenges in August

preface

An Array is an ordered collection of data. Each value in the Array is called an Element of the Array, and the encoding of each Element is called an Index of the Array. JavaScript is a weakly typed language, with loose array structure and syntax conventions. The value of each element can be of any type, and different elements of the same array can store different types of data. The array length is not fixed, and can be arbitrarily stretched and contracted. JavaScript does not support two-dimensional or multi-dimensional arrays, and complex multi-dimensional arrays can be created indirectly by including arrays with elements.

Commonly used method

indexOf

The indexOf method, used to find if the array contains the specified value, returns the indexOf the specified value, or -1 if none exists

const list = ['A'.'B'.'C'];
console.log(list.indexOf('C')); / / 2
console.log(list.indexOf('D')); // -1
Copy the code
map

The map method returns a new array by calling each element in the specified array.

const arr = [1.2.3];
const arr2=arr.map((item) = >{
    return item*2;
});
console.log(arr) / / [1, 2, 3]
console.log(arr2) / / minus [2]
Copy the code
push

The push method, which adds an element to the end of an array, returns the length of the array.

const arr = [1.2.3];
arr.push(4.5.6)
console.log(arr) / / [6]
Copy the code
unshift

The unshift method adds an element to the front of an array, returning the length of the array.

const arr = [1.2.3];
arr.unshift(4.5.6)
console.log(arr) / /,5,6,1,2,3 [4]
Copy the code
pop

The pop method fetches the last element of the array (tail) and returns the retrieved value.

const arr = [1.2.3];
const last = arr.pop()
console.log(arr) / / [1, 2]
console.log(last) / / 3
Copy the code
shift

The shift method, used to remove the uppermost element in the array (the header), returns the retrieved value.

const arr = [1.2.3];
const first = arr.shift()
console.log(arr) / / [2, 3]
console.log(first) / / 1

Copy the code
join

The join method concatenates the elements in an array using the specified concatenate, returning the concatenation to a good string.

const arr = [1.2.3];
const res = arr.join('&')
console.log(res) / / "1 & 2 & 3"
Copy the code
sort

The sort method is used to sort an array, by default by string, or to pass in a function that returns a new array.

const arr = [9.12.3.21.18];
const res = arr.sort((a,b) = >a-b)
console.log(res) / /,9,12,18,21 [3]
Copy the code
filter

The filter method checks the array for all elements that match the criteria and returns all elements in the array that match the criteria. Returns a new array without changing the original array.

const arr = [1.2.3.4.5.6.7.8.9.10];
const arr2 = arr.filter(item= >item%2! = =0)
console.log(arr2) / /,3,5,7,9 [1]
Copy the code
some

The some method checks whether one or more of the elements in the array meet the criteria and returns true if so, false otherwise.

const arr = [9.12.3.21.18];
const res = arr.some((item) = >item<5)
console.log(res) // true
Copy the code
every

The every method checks whether all elements in the array meet the criteria and returns true if so, false otherwise.

const arr = [9.12.3.21.18];
const res = arr.every((item) = >item<5)
console.log(res) // false
Copy the code
forEach

The forEach method, similar to the map object above, also makes a function call on each element in the array. ForEach does not return a value (undefined).

const arr = [1.2.3];
arr.forEach((item) = >{
    console.log(item*2)});/ / 2
/ / 4
/ / 6
Copy the code
reduce

The reduce method executes the Reducer function you provide on each element in the array, and summarizes the results into a single return value.

const list = [1.2.3]
const sum = list.reduce(((accumulator, currentValue) = >accumulator + currentValue)
, 0);
console.log(sum) / / 6
Copy the code
reverse

The reverse method, which reverses the position of an element in an array. Returns an inverted array that modifies the original array.

const arr = ['ABC'.'age'.12.3];
const res = arr.reverse()
console.log(res) / / [3, 12, "age", "ABC"]
Copy the code
includes

Includes method that checks whether the array contains a specified value and returns true if it does, false otherwise.

const arr = ['A'.'B'.'C'];
console.log(arr.includes('A')) // true
console.log(arr.includes('D')) // false
Copy the code

The last

The above JavaScript array methods, is based on the daily project development, involving more methods to sum up the operation. I hope this article will be of better help to your daily development!

If you have any more common array methods, please write them in the comments section.