numberResponsive operations of groups
//('123');//Add an element at the end
//();//Delete an element from the end
//('111');//Add an element at the beginning
//();//Delete an element from the beginning
//(1,2);//Delete two elements from the element with subscript 1
//(1,2,'777','888');//Delete two elements from the element with subscript 1 and insert a new element
//(1, 0, '777', '888'); //Insert two elements at the subscript of 1
//(2);//Keep the first 2 elements and delete the others
Advanced functions
1. filter filter function
filter is also a common operation. It is used to filter out certain elements of Array and then return the remaining elements.
The filter applies the passed function to each element in sequence, and then decides whether the return value is true or false to retain or discard the element.
const nums = [2,3,5,1,77,55,100,200]; let newArray = (function (n) { // Less than 100 is true, enter the newArray array return n < 100; }) (newArray);//[2,3,5,1,77,55]
2. Map higher-order functions
The map function will also traverse each item in the array. The incoming callback function is a parameter. num is each item in the map traversal. The return value of the callback function function function will be added to the new array
const nums = [2,3,5,1,77,55,100,200]; let newArray = (function (n) { // Less than 100 is true, enter the newArray array return n < 100; }) (newArray);//[2,3,5,1,77,55]
3. Reduce higher-order functions
Reduce function will also traverse each item in the array, and pass the callback function and '0' as parameters. 0 means that the initial value of preValue in the callback function is 0. The parameter preValue in the callback function is the value returned by each callback function function, and currentValue is the current value
const nums = [2,3,5,1,77,55,100,200]; let new3Array = (function (preValue,currentValue) { //The array is [2,3,5,1,77,55,100,200], then the first return value of the callback function is 0+2=2, the second preValue is 2, the return value is 2+3=5, and so on until the traversal is completed return preValue+currentValue; },0);//The second parameter 0 is the initial value of preValue(new3Array);//443
4. sort sort algorithm
Because Array's sort() method converts all elements into String by default and then sorts, the result '10' is ranked before '2', because the character '1' is smaller than the ASCII code of the character '2'. If you don’t know the default sorting rules of the sort() method, you will be able to sort the numbers directly and you will definitely fall into the pit!
//To sort by number size, we can write this:var arr = [10, 20, 1, 2]; (function (x, y) { if (x < y) { return -1; } if (x > y) { return 1; } return 0; }); // [1, 2, 10, 20] //If you want to sort in reverse order, we can put the large number in front:var arr = [10, 20, 1, 2]; (function (x, y) { if (x < y) { return 1; } if (x > y) { return -1; } return 0; }); // [20, 10, 2, 1]
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.