Array

array.slice(start,end)

The slice() method returns a new array object that is a shallow copy of the array determined by begin and end (begin, but not end; The value can be: [start, end);) the original array will not be changed.

Values start at subscript 0;

If the number is negative, start with the reciprocal (-1 is the first reciprocal);

If end is omitted, slice extracts all the way to the end of the array;

Start is included, end is not included (if end is larger than array, end is taken to the end);

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console.log(animals.slice(2)); Array ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)); Array ["camel", "duck"] console.log(animals.slice(1, 5)); / / print results: Array [" bison ", "camel", "duck", "elephant"]Copy the code

arr.some()

Some () performs a callback function once for each element in the array until a value is found that causes the callback to return a “true” value (which can be converted to a Boolean value of true). If such a value is found, some() will immediately return true. Otherwise, some() returns false. Callback will only be called on indexes that “have a value”, not indexes that have been deleted or never assigned a value.

const array = [1, 2, 3, 4, 5]; // checks whether an element is even const even = (element) => element % 2 === = 0; // checks whether an element is even const even = (element) => element % 2 === = 0; console.log(array.some(even)); // Prints the result: trueCopy the code

arr.every()

The every method executes the callback function once for each element in the array until it finds an element that causes the callback to return Falsy. If one is found, the every method will immediately return false. Otherwise, callback returns true for each element, and every returns true. Callback will only be called for indexes that have already been assigned a value. Not called for indexes that are deleted or never assigned.

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true
Copy the code

arr.sort()

The sort() method sorts the elements of an array using an in-place algorithm and returns the array. The default sort order is built when converting elements to strings and then comparing their UTF-16 code unit value sequences

Because it is implementation-dependent, the temporal and spatial complexity of sorting cannot be guaranteed.

const months = ['March', 'Jan', 'Feb', 'Dec']; months.sort(); console.log(months); // expected output: Array ["Dec", "Feb", "Jan", "March"] const array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1); Expected Output: Array [1, 100000, 21, 30, 4] // Expected output: Array [1, 100000, 21, 30, 4] numbers.sort(function(a, b) { return a - b; }); console.log(numbers); Var numbers = [4, 2, 5, 1, 3]; numbers.sort((a, b) => a - b); console.log(numbers);Copy the code

array.splice(start[, deleteCount[, item1[, item2[, …]]]])

Start: what element is added after deleteCount

Var myFish = ['angel', 'Clown ', 'drum',' Sturgeon ']; var removed = myFish.splice(2, 1, "trumpet"); // myFish: [" Angel ", "Clown ", "trumpet"," Sturgeon "] ["drum"] // Delete 2 elements from bit 0 and insert "parrot", "anemone" and "blue" var myFish = [' Angel ', 'Clown ', 'trumpet',' Sturgeon ']; var removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue'); // myFish after operation: ["parrot", "anemone", "Blue ", "trumpet"," Sturgeon "] Var myFish = ['angel', 'Clown ', 'mandarin', 'sturgeon']; var removed = myFish.splice(2); // myFish: ["angel", "Clown "] // Deleted element: ["mandarin", "sturgeon"]Copy the code

arr.join

var arr = ['a','b','c'] var str = arr.join('/') var str2 = str.split('/') console.log(str) // a/b/c console.log(str2) //  ['a','b','c']Copy the code