1. Basic methods:
Push: Adding a return value to the end of the array is the new length of the array
Unshift: Adding a return value to the beginning of the array is the new length of the array
Pop: Delete the end item of an array the return value is a deleted array item
Shift: Delete Array start item returns the beginning item that was deleted
Splice: Adding and deleting, powerful function
Slice: Copy array return value is copied to the new array after the value is written does not contain the last item being copied
2. Stitching:
Concat: Stitching an array together with another array to return a well-stitched array
Join: Stitch Each item in the array into a string by the specified delimiter
3. Sort:
Reverse: Reverse Array return value reverse array original array change
Sort: Bubble sort based on anonymous function b-a reverse A-B ascending
4. Poor compatibility:
INDEXOF: Returns the index of the get item in the array
LastIndexOf: Returns the last index that gets the item that appears in the array
ForEach: Looping through an array parameter is an anonymous function returned by default to undefined
Map: Looping through an array parameter is an anonymous function
Filter: Create a new array that matches the filter criteria
Reduce: Each value in the array (left to right) starts merging and ends with a value.
1. Basic methods:
push: Adding a return value to the end of the array is the new length of the array
unshift: Adding a return value to the beginning of the array is the new length of the array
var array = ["A", "B", "C", "D", "E", "F"]; var last = Array.unshift ("Begin"), Console.log (array) // ["Begin", "a", "B", "C", "D "," E "," F "]
Pop: Delete the end item of an array the return value is a deleted array item
var array = ["A", "B", "C", "D", "E", "F"]; var last = array.pop (); Console.log (array) //["A", "B", "C", "D", "E"]
shift: Delete Array start item returns the beginning item that was deleted
var array = ["A", "B", "C", "D", "E", "F"]; var last = array.shift (); Console.log (array) // ["B", "C", "D", "E", "F"]
Splice: Adding and deleting, powerful function
(1Delete: Simply provide two parameters, the position of the first item to delete, and the number of elements to delete, and return the deleted element array:varnum = [1,2,3,4,5];varNewnum = Num.splice (); Console.log (num); //[1,4,5]Console.log (Newnum);//[2,3](2Insert: Provide multiple parameters, the first parameter is the position to be inserted, the second is 0 to delete 0, followed by the element to be inserted, can be multiple, because delete 0, so return an empty array; * Four parameters, the second is 0 for inserting
varnum = [1,2,3,4,5];varNewnum = Num.splice (1,0, "Tom", "Jerry"); Console.log (num); //[1, "Tom", "Jerry", 2, 3, 4, 5]Console.log (Newnum);//[](3Replace: Provide multiple parameters, the first parameter is the position to be inserted, the second is the number of deletes, followed by the element to be inserted, can be multiple, return the deleted array;
* Four parameters, the second greater than 0 represents the replacement, is how much to replace the number, the first parameter represents a few places from the subscript to start replacing varnum = [1,2,3,4,5];varNewnum = Num.splice ("Tom", "Jerry"); Console.log (num); //[1, "Tom", "Jerry", 4, 5]Console.log (Newnum);//[2,3]
Slice: The copy array return value is a new array copied to, and the method does not modify the original array
* Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.
* If End is not specified, then the slice () method selects all elements from start to the end of the array.
var array = ["A", "B", "C", "D", "E", "F"];console.log (Array.slice (-1)) //["F"] Console.log (Array.slice ( -2)) //["E", "F"]Console.log (Array.slice (1)) // ["B", "C", "D", "E", "F"]Arrays.slice (1,4) // ["B", "C", "D"]
2. Stitching:
concat: Stitching an array together with another array to return a well-stitched array
var array = ["A", "B", "C"]; var array1 = ["A", "B", "C"]; var array2 = array.concat (array1) console.log (array2) // ["A", "B", "C", "A", "B", " C "]
Join: Stitch each item in the array into a string by the specified delimiter
var array = ["A", "B", "C", "D", "E", "F"]; var array1 = Array.join (""); //" abcdef " var array2 = Array.join ("_"); //" A_b_c_d_e_f "
3. Sort:
Reverse: Reverse array return value reverse array original array change
var arr = [1, 2, 3, 4, 5];arr.reverse () // [5, 4, 3, 2, 1]
sort: Bubble sort based on anonymous function b-a reverse-A-B ascending, but note that this method is sorted by ASCII code (not very practical)
var arr = [1,3,7,5,14,24];arr.sort (); Console.log (arr); ==>[1,14,24,3,5,7]
4. Common methods:
indexOf: Returns the index of the get item in the array (the first occurrence of the index)
var arr = [' Apple ', ' orange ', ' pear ']; Console.log (// return index 1
lastIndexOf: Returns the last index that gets the item that appears in the array
var arr = [' Apple ', ' orange ', ' orange ', ' pear ']; Console.log ("Found:", Arr.lastindexof ("orange")); // back to index 2
ForEach: Looping through an array parameter is an anonymous function returned by default to undefined
var array1 = [1,2,3,4,5,6]; var x = Array1.foreach (function(value,index,array) { = value+1; return array;}); Console.log (// Whatever the above returns, X is undefined//[2,3,4,5,6,7]
map: Looping through an array parameter is an anonymous function
var array1 = [1,2,3,4,5,6]; var x = Array1.map (function(value,index,array) { return value+1;}); Console.log ("new Array" +x); // [2,3,4,5,6,7]; Console.log ("original array" +array1); // [1,2,3,4,5,6];
Filter: Create a new array that matches the filter criteria
var arr = [ {"name": "Apple", "Count": 2}, {"name": "Orange", "Count": 5}, { "name": "Pear", "Count": 3}, {"name": "Orange", "count": +},]; var newArr = Arr.filter (function(item) { return Item.name = = = = " Orange ";}); Console.log (// [
{"Name": "Orange", "Count": 5},
{"Name": "Orange", "Count": 16}
]
reduce: Each value in the array (left to right) starts merging and ends with a value.
vararr = [1,2,3,4];varNewarr = Arr.reduce (function(x, y) {returnX * 10 +y; }); Console.log (Newarr)//1234/** * Reduce (callback, InitialValue) will pass in two variables * callback function (callback), initial value (initialvalue) * Generally speaking prev is from the first element in the array start, Next is the second element. * But when you pass in the initial value (InitialValue), the first prev will be Initivalvalue,next will be the first element in the array. */vararr = ["Apple", "orange"];functionNopassvalue () {returnArr.reduce (function(Prev,next) {Console.log ("Prev:", prev);//Prev:appleConsole.log ("Next:", next);//Prev:orange returnPrev + "" +next;//"Apple Orange" });} Nopassvalue ();vararr = ["Apple", "orange"];functionPassvalue () {returnArr.reduce (function(Prev,next) {Console.log ("Prev:", prev);//{} {"Apple"}Console.log ("Next:", next);//"Apple" "Orange "Prev[next]= 1;//in this assignment {}["Apple"]=1-->{apple:1} returnPrev//{apple:1, orange:1} },{});} Passvalue ();//as a result, you can: count the number of distinct words in an arrayvararr = ["Apple", "orange", "apple", "orange", "pear", "orange"];functiongetwordcnt () {returnArr.reduce (function(Prev,next) {Prev[next]= (Prev[next] + 1) | | 1; returnprev; },{});} Console.log (getwordcnt ()); //{apple:2, orange:3, pear:1}
The final result of a classmate is indicated as followsvarresult =[{subject:' Math ', Score:88}, {subject:' Chinese ', Score:95}, {subject:' 中文版 ', Score:80}]; How to ask for the student's total? Obviously, the use of a For loop can be very simple to draw a conclusionvarsum = 0; for(vari=0; i<result.length; i++) {sum+=Result[i].score;} But our aim is to abandon the for loop, so using reduce to fix this problem, the following equivalent setting is initially divided into 0,varsum = Result.reduce (function(prev, cur) {returnCur.score +prev;},0);
A. Common methods for JavaScript arrays