A, concat ()

The concat() method is used to join two or more arrays. This method does not alter the existing array and only returns a copy of the concatenated array.

var arr1 = [1.2.3];
var arr2 = [4.5];
var arr3 = arr1.concat(arr2);
console.log(arr1); / / [1, 2, 3]
console.log(arr3); //[1, 2, 3, 4, 5]
Copy the code

Second, the join ()

The join() method is used to put all the elements of an array into a single string. Elements are separated by the specified delimiter. By default, the element is separated by the ‘,’ sign, and the original array is not changed.

var arr = [2.3.4];
console.log(arr.join()); / / 2 and 4
console.log(arr); / / [2, 3, 4]
Copy the code

Three, push ()

The push() method adds one or more elements to the end of the array and returns a new length. Add at the end, returns the length, changes the array.

var a = [2.3.4];
var b = a.push(5);
console.log(a); / / 5-tetrafluorobenzoic [2]
console.log(b); / / 4
Copy the code

Five, the shift ()

The shift() method removes the first element from an array and returns the value of the first element. Returns the first element, changing the array.

var arr = [2.3.4];
console.log(arr.shift()); / / 2
console.log(arr); / / [3, 4]
Copy the code

Six, unshift ()

The unshift() method adds one or more elements to the beginning of the array and returns the new length. Returns the new length, changing the original array.

var arr = [2.3.4.5];
console.log(arr.unshift(3.6)); / / 6
console.log(arr); //[3, 6, 2, 3, 4, 5]
Copy the code

Tip: This method can pass no arguments, which means no elements are added.

Seven, slice ()

The slice() method returns a new array containing elements from the arrayObject from start to end (excluding the element). Returns the selected element. This method does not modify the array.

var arr = [2.3.4.5];
console.log(arr.slice(1.3)); / / [3, 4]
console.log(arr); / / 5-tetrafluorobenzoic [2]
Copy the code

Eight, splice ()

The splice() method removes zero or more elements starting at index and replaces those deleted elements with one or more values declared in the argument list. If an element is deleted from an arrayObject, an array containing the deleted element is returned. The splice() method modifies the array directly.

var a = [5.6.7.8];
console.log(a.splice(1.0.9)); / / []
console.log(a); // [5, 9, 6, 7, 8]
var b = [5.6.7.8];
console.log(b.splice(1.2.3)); / / [6, 7)
console.log(b); / / [5, 3, 8]
Copy the code

Substring () and substr()

Similarity: If you write only one argument, they both work the same way: they both intercept the string fragment from the current subscript to the end of the string.

substr(startIndex);
substring(startIndex);
var str = '123456789';
console.log(str.substr(2)); / / "3456789"
console.log(str.substring(2));/ / "3456789"
Copy the code

Difference: The second argument substr (startIndex,lenth) : The second argument is to truncate the length of the string (truncate a length of string from the starting point); Substring (startIndex, endIndex) : The second argument is to truncate the final index of the string (truncate the string between 2 positions, ‘header without tail’).

console.log("123456789".substr(2.5)); / / "34567"
console.log("123456789".substring(2.5));/ / "345"
Copy the code

A. sort B. sort C. sort D. sort

Sort by Unicode code position, ascending by default

var fruit = ['cherries'.'apples'.'bananas'];
fruit.sort(); // ['apples', 'bananas', 'cherries']
var scores = [1.10.21.2];
scores.sort(); // [1, 10, 2, 21]
Copy the code

11, the reverse ()

The reverse() method is used to reverse the order of elements in an array. It returns the array upside down, changing the original array.

var arr = [2.3.4];
console.log(arr.reverse()); / / [4, 3, 2)
console.log(arr); / / [4, 3, 2)
Copy the code

IndexOf and lastIndexOf both take two arguments: search value, search start position does not exist, return -1; Yes, return to position. IndexOf is looking backwards, lastIndexOf is looking backwards. indexOf

var a = [2.9.9];
a.indexOf(2); / / 0
a.indexOf(7); // -1
 
if (a.indexOf(7) = = =- 1) {
 // element doesn't exist in array
}
lastIndexOf
 
var numbers = [2.5.9.2];
numbers.lastIndexOf(2);  / / 3
numbers.lastIndexOf(7);  // -1
numbers.lastIndexOf(2.3); / / 3
numbers.lastIndexOf(2.2); / / 0
numbers.lastIndexOf(2.2 -); / / 0
numbers.lastIndexOf(2.- 1); / / 3
Copy the code

Every executes the given function on each item of the array, and returns true on each item

function isBigEnough(element, index, array) {
 return element < 10;
} 
[2.5.8.3.4].every(isBigEnough); // true
Copy the code

Some Returns true if the given function is executed on each item of the array

function compare(element, index, array) {
 return element > 10;
}  
[2.5.8.1.4].some(compare); // false
[12.5.8.1.4].some(compare); // true
Copy the code

Filter executes the given function on each item of the array, returning an array of the items whose result is true

var words = ["spray"."limit"."elite"."exuberant"."destruction"."present"."happy"];
var longWords = words.filter(function(word){
 return word.length > 6;
});
// Filtered array longWords is ["exuberant", "destruction", "present"]
Copy the code

16, the map

The given function is run on each item of the array, returning the result of each function call to form a new array

var numbers = [1.5.10.15];
var doubles = numbers.map(function(x) {
  return x * 2;
});
// doubles is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]
Copy the code

ForEach array traversal

const items = ['item1'.'item2'.'item3'];
const copy = [];  
items.forEach(function(item){
 copy.push(item)
});
Copy the code

ES6 adds new methods to manipulate arrays

1, the find () :

Pass in a callback function that finds the first element in the array that matches the current search rule, returns it, and terminates the search.

const arr = [1."2".3.3."2"]
console.log(arr.find(n= > typeof n === "number")) / / 1
Copy the code

2, findIndex () :

We pass in a callback function that finds the first element in the array that matches the current search rule, returns its subscript, and terminates the search.

const arr = [1."2".3.3."2"]
console.log(arr.findIndex(n= > typeof n === "number")) / / 0
Copy the code

3, the fill () :

To replace an element in an array with a new element, you can specify the range of substitution subscripts.

arr.fill(value, start, end)
Copy the code

4, copyWithin () :

Select an index of the array and copy the array elements from there, starting at 0 by default. You can also specify a range of elements to copy.

arr.copyWithin(target, start, end)
const arr = [1.2.3.4.5]
console.log(arr.copyWithin(3))
 // [1,2,3,1,2] copies the array from the element with subscript 3, so 4, 5 are replaced with 1,2
const arr1 = [1.2.3.4.5]
console.log(arr1.copyWithin(3.1)) 
// [1,2,3,2] copies the array from the element with subscript 3, specifying that the first element to be copied has subscript 1, so 4, 5 are replaced with 2,3
const arr2 = [1.2.3.4.5]
console.log(arr2.copyWithin(3.1.2)) 
// [1,2,3,2,5] copies the array from the element with subscript 3, specifying that the first element to be copied is subscript 1 and ends at position 2, so 4 is replaced with 2
Copy the code

5, the from

Convert array-like objects and iterable objects into true arrays

const bar = ["a"."b"."c"];
Array.from(bar);
// ["a", "b", "c"]
 
Array.from('foo');
// ["f", "o", "o"]
Copy the code

Six of them,

Used to convert a set of values into an array. The main purpose of this method is to complement the Array constructor Array(). Array() behaves differently because of the number of arguments.

Array(a)/ / []
Array(3) / / /,,,
Array(3.11.8) / / [3, 11, 8]
Array.of(7);    / / [7]
Array.of(1.2.3); / / [1, 2, 3]
Array(7);     // [,,,,,,]
Array(1.2.3);  / / [1, 2, 3]
Copy the code

Entries () returns iterator: returns key-value pairs

/ / array
const arr = ['a'.'b'.'c'];
for(let v of arr.entries()) {
 console.log(v)
}
// [0, 'a'] [1, 'b'] [2, 'c']
 
//Set
const arr = new Set(['a'.'b'.'c']);
for(let v of arr.entries()) {
 console.log(v)
}
// ['a', 'a'] ['b', 'b'] ['c', 'c']
 
//Map
const arr = new Map(a); arr.set('a'.'a');
arr.set('b'.'b');
for(let v of arr.entries()) {
 console.log(v)
}
// ['a', 'a'] ['b', 'b']
Copy the code

Values () returns iterator: returns the value of the key-value pair

/ / array
const arr = ['a'.'b'.'c'];
for(let v of arr.values()) {
 console.log(v)
}
//'a' 'b' 'c'
 
//Set
const arr = new Set(['a'.'b'.'c']);
for(let v of arr.values()) {
 console.log(v)
}
// 'a' 'b' 'c'
 
//Map
const arr = new Map(a); arr.set('a'.'a');
arr.set('b'.'b');
for(let v of arr.values()) {
 console.log(v)
}
// 'a' 'b'
Copy the code

Keys () returns the iterator: returns the key of the key-value pair

/ / array
const arr = ['a'.'b'.'c'];
for(let v of arr.keys()) {
 console.log(v)
}
/ / 0 1 2
 
//Set
const arr = new Set(['a'.'b'.'c']);
for(let v of arr.keys()) {
 console.log(v)
}
// 'a' 'b' 'c'
 
//Map
const arr = new Map(a); arr.set('a'.'a');
arr.set('b'.'b');
for(let v of arr.keys()) {
 console.log(v)
}
// 'a' 'b'
Copy the code

10 and includes

To determine whether the element is present in the array, use parameters such as the value searched and the starting position to replace the ES5 era indexOf. IndexOf determines whether an element is a NaN, which is incorrect.

var a = [1.2.3];
a.includes(2); // true
a.includes(4); // false
Copy the code