Returns a new array without changing the original array

join

Puts all the elements of an array into a string, and returns the string.

/ / are hundreds/join. Js
const arr = ['pr'.'is'.18];

console.log(arr.join(' ')); // pr is 18
console.log('= >');
console.log(arr); // [ 'pr', 'is', 18 ]
Copy the code

concat

Concatenate multiple (including two) arrays, both sides of the original array will not change, return a copy of the array concatenated, can continue concat.

/ / are hundreds/concat. Js
const arr = [1.2.3.4];
const arr1 = ['pr'.'is'.'a'.'boy'];
const arr2 = [5.6.7];

console.log(arr.concat(arr1, arr2).concat(8.9)); // [1, 2, 3, 4, 'pr', 'is', 'a', 'boy', 5, 6, 7, 8, 9 ]
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

slice

From start to end ([) close left to open right, that is, not including end) select a shallow copy of part of the array to a new array.

/ / are hundreds/slice. Js
const arr = [0.1.2.3.4.5.6.7.8.9];

console.log(arr.slice(1.5)); // [1, 2, 3, 4]
console.log('= >');
console.log(arr); / /,1,2,3,4,5,6,7,8,9 [0]
Copy the code

Slice (1, 5) shows a maximum of 4 (5-1) elements, starting with the first bit.

map

Creates a new array and returns each element of the new array executed by each element in the original array, where the original array is unchanged.

/ / are hundreds/map. Js
const arr = [1.2.3.4];

console.log(arr.map(i= > i * 10 - 5)); // [5, 15, 25, 35]
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

every

Checks whether all elements of an array match the specified condition.

  • If one element in the array is detected to be unsatisfied, the entire expression is returnedfalseAnd the remaining elements will not be detected;
  • Returns if all elements meet the criteriatrue;
/ / are hundreds every. Js
const arr = [1.2.3.4];

console.log(arr.every(i= > i > 2)); // false
console.log(arr.every(i= > i > 0)); // true
console.log([].every(i= > i === 'pr')); // true
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

some

Checks whether an element in an array meets a specified condition.

  • If one element satisfies the condition, the expression returnstrue, the remaining elements will not be tested;
  • Returns if no element satisfies the conditionfalse;
/ / are hundreds/some. Js
const arr = [1.2.3.4];

console.log(arr.some(i= > i > 4)); // false
console.log(arr.some(i= > i > 0)); // true
console.log([].some(i= > i === 'pr')); // false
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

filter

Create a new array. The elements in the new array are checked by checking all the elements that match the criteria.

/ / are hundreds/filter. Js
const arr = [1.2.3.4];

console.log(arr.filter(i= > i > 2)); / / [3, 4]
console.log([].filter(i= > i === 'pr')); / / []
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

forEach

Undefiend is used to call each element of the array and pass the elements to the callback function.

/ / are hundreds/forEach. Js
const arr = [1.2.3.4];
const copy = [];

console.log(arr.forEach(i= > {
    copy.push(i * 2);
}));
console.log(copy); // [2, 4, 6, 8]
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

reduce

Receiving a function as an accumulator, each value in the array (from left to right) is reduced to a value. An empty array does not execute a callback;

/ / are hundreds/reduce. Js
const arr = [1.2.3.4];

console.log(arr.reduce((prev, cur) = > prev + cur, 0)); / / 10
console.log('= >');
console.log(arr); // [1, 2, 3, 4]
Copy the code

Returns a new array, changing the original array

pop

Removes the last element of the array and returns that element (the deleted element).

  • If the array is empty, do not change the array, returnundefined;
/ / are hundreds/pop. Js
const arr = [1.2.3.4];
const arr1 = [];

console.log(arr.pop()); / / 4
console.log(arr1.pop()); // undefined
console.log('= >');
console.log(arr); // [1, 2, 3]
console.log(arr1); / / []
Copy the code

push

Adds one or more elements to the end of an array. Returns the changed array length.

/ / are hundreds/push. Js
const arr = [1.2.3.4];

console.log(arr.push(5)); / / 5
console.log(arr.push([1.2])); / / 6
console.log('= >');
console.log(arr); // [1, 2, 3, 4, 5, [1, 2]]
Copy the code

shift

Removes the first element of the array and returns it.

/ / are hundreds/shift. Js
const arr = [1.2.3.4];

console.log(arr.shift()); / / 1
console.log('= >');
console.log(arr); // [2, 3, 4]
Copy the code

unshift

Adds one or more elements to the beginning of an array. Returns the changed length of the array.

/ / are hundreds/unshift. Js
const arr = [1.2.3.4];

console.log(arr.unshift(5.6)); / / 6
console.log(arr.unshift([1.2])); / / 7
console.log('= >');
console.log(arr); // [[1, 2], 5, 6, 1, 2, 3, 4]
Copy the code

reverse

Reverses the position of an element in an array, returning a reference to the array.

/ / are hundreds/reverse. Js
const arr = [1.2.3.4];
const hello = 'hello';
const helloArray = hello.split(' ');

console.log(helloArray.reverse().join(' ')); // olleh
console.log(arr.reverse()); // [4, 3, 2, 1]
console.log('= >');
console.log(arr); // [4, 3, 2, 1]
console.log(helloArray); // [ 'o', 'l', 'l', 'e', 'h' ]
Copy the code

sort

Sort the elements of an array and return an array. Sorting is not necessarily stable. The default sort order is based on string Unicode code points.

/ / are hundreds/sort. Js
const arr = [1.2.3.4.10.12.22];

console.log(arr.sort()); // [1, 10, 12, 2, 22, 3, 4];
console.log('= >');
console.log(arr); // [1, 10, 12, 2, 22, 3, 4];
Copy the code

splice

Add/remove items to the array, and then return the deleted items.

/ / are hundreds/splice. Js
const arr = [1.2.3.4];

console.log(arr.splice(1.2.10.12)); // [2, 3]
console.log('= >');
console.log(arr); // [1, 10, 12, 4]
Copy the code

This code Github

You can…

  • Common algorithms for Javascript arrays