This article mainly introduces the relevant information about common methods of JavaScript array, which is very detailed. For more information, see
Determine whether an object is an Array: instanceof, Array. isArray ()
The instanceof operator can be used for a webpage or a global scope.
If (value instanceof Array) {// determines whether the value is an Array.
}
The instanceof operator assumes that there is only one global execution environment. if a webpage contains multiple frameworks, use the Array. isArray () method added by ECMAScript5.
If (Array. isArray (value) {// determines whether the value is an Array.
}
The Array. isArray () method supports IE9 +, Firefor 4 +, Safari5 +, Opera 10.5 +, and Chrome.
If you want to check the array in the browser that does not implement this method, use:
If (Object. prototype. toString. call (value) = "[object Array]") {
}
Convert the array to a string: toLocaleString (), toString (), valueOf (), join ()
The code is as follows:
Var test = ['A', 'B', 'C'];
Alert (test. toString (); // a, B, c
Alert (test. toLocaleString (); // a, B, c
Alert (test. valueOf (); // a, B, c
Alert (test); // The toString () method is called by default in a, B, and c.
Alert (test. join (','); // a, B, c
Alert (test. join ('|'); // a | B | c
Add and remove array elements: push (), pop (), unshift (), shift ()
The push () method can accept any number of parameters, add them to the end of the array one by one, and return the modified length of the array.
The pop () method removes the last entry from the end of the array and returns the removed entry.
The unshift () method adds any number of parameters to the front of the array and returns the length of the new array.
The shift () method can remove the first entry in the array and return the removed entry.
The code is as follows:
Var test = [];
Var count = test. push ('A', 'B'); // add one by one from the end of the array
Count = test. push ('C ');
Alert (count); // 3
Alert (test );//
Var item = test. pop ();
Alert (item); // c
Alert (test. length); // 2
Sorting method: reverse () and sort ()
The reverse () method will reverse the array order and operate the array itself.
By default, the sort () method sorts the array items in ascending order and operates the array itself.
The code is as follows:
Var test = [1, 2, 3, 4, 5];
Test. reverse ();
Alert (test); // 5, 4, 3, 2, 1
Var test2 = [0, 1, 5, 10, 15];
Test2.sort ();
Alert (test2); // The toString () method of each array item is called to compare strings to determine the sorting. So here sorting is string sorting.
The sort () method can also pass in a comparison function.
The comparison function returns a negative number if the first parameter is located before the second parameter. if the two parameters are equal, 0 is returned. if the first parameter is located after the second parameter, a positive number is returned.
The code is as follows:
Function compare (value1, value2 ){
If (value1 Return-1;
} Else if (value1> value2 ){
Return 1;
} Else {
Return 0;
}
}
Var test = [0, 1, 5, 10, 15];
Test. sort (compare );
Alert (test); // 0, 1, 5, 10, 15
Operation method: concat (), slice (), splice ()
The concat () method is used to connect two or more arrays. This method does not change the existing array, but only returns a copy of the connected array. Returns a new array.
The code is as follows:
Var a = [1, 2, 3];
Alert (a. concat (); //, 5
Var arr = new Array (3)
Arr [0] = "George"
Arr [1] = "John"
Arr [2] = "Thomas"
Var arr2 = new Array (3)
Arr2 [0] = "James"
Arr2 [1] = "Adrew"
Arr2 [2] = "Martin"
Alert (arr. concat (arr2 ));
// George, John, Thomas, James, Adrew, Martin
Var arr = new Array (3)
Arr [0] = "George"
Arr [1] = "John"
Arr [2] = "Thomas"
Var arr2 = new Array (3)
Arr2 [0] = "James"
Arr2 [1] = "Adrew"
Arr2 [2] = "Martin"
Var arr3 = new Array (2)
Arr3 [0] = "William"
Arr3 [1] = "Franklin"
Alert (arr. concat (arr2, arr3 ))
// George, John, Thomas, James, Adrew, Martin, William, Franklin
The slice () method returns the selected element from an existing array. Returns a new array containing elements in the arrayObject from start to end (excluding this element.
The code is as follows:
Var test = ['A', 'B', 'C', 'D', 'E'];
Var arr1 = test. slice (1 );
Var arr2 = test. slice (1, 4 );
Alert (arr1); // B, c, d, e
Alert (arr2); // B, c, d
The splice () method adds/deletes a project to/from the array, and then returns the deleted project. Operate the array itself.
The first parameter is the start position, the second parameter is the number of interceptions, and the third parameter is the new element to be appended.
The code is as follows:
// Delete
Var test = ['A', 'B', 'C'];
Var removed = test. splice () // delete the first item
Alert (test); // B, c
Alert (removed); // a returns the deleted item
// Insert
Var test2 = ['A', 'B', 'C'];
Var removed2 = test2.splice (, 'D', 'E') // Insert d, e from position 1
Alert (test2); // a, d, e, B, c
Alert (removed2) // empty array
// Replace
Var test3 = ['A', 'B', 'C'];
Var removed3 = test3.splice (, 'D', 'E') // Insert d, e from position 1
Alert (test3); // a, d, e, c
Alert (removed3) // B
Location method: indexOf (), lastIndexOf ()
ECMAScript5 provides methods to support browsers: IE9 +, Firefox 2 +, Safari 3 +, Opera 9.5 +, and Chrome
The indexOf () method returns the position of the first occurrence of a specified string value.
The lastIndexOf () method returns the last position of a specified string value and searches forward from the specified position in the string.
When one parameter is used, it indicates the value to be searched. when the index position (starting from 0) and two parameters are returned, the first parameter indicates the start position and the second parameter indicates the value to be searched.
The code is as follows:
Var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Alert (numbers. indexOf (4); // 3
Alert (numbers. lastIndexOf (4); // 5
Alert (numbers. IndexOf (4, 4); // 5
Alert (numbers. lastIndexOf (4, 4); // 3
Iteration methods: every (), filter (), forEach (), map (), and some ()
ECMAScript5 provides methods to support browsers: IE9 +, Firefox 2 +, Safari 3 +, Opera 9.5 +, and Chrome
Every (): Run the given function for each item in the array. if this function returns true for each item, true is returned.
Filter (): If you run a given function for each item in the array, returning this function returns an array consisting of true items.
ForEach (): Run the given function for each item in the array. this method has no return value.
Map (): runs a given function for each item in the array and returns an array composed of the results of each function call.
Some (): Run the given function for each item in the array. if this function returns true for any item, true is returned.
The preceding functions do not modify the values contained in the array.
The code is as follows:
Var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
// Every ()
Var everyResult = numbers. every (function (item, index, array ){
Return (item> 2 );
})
Alert (everyResult); // false
// Some ()
Var someResult = numbers. some (function (item, index, array ){
Return (item> 2 );
})
Alert (someResult); // true
// Filter ()
Var filterResult = numbers. filter (function (item, index, array ){
Return (item> 2 );
})
Alert (filterResult); // [3, 4, 5, 4, 3]
// Map ()
Var mapResult = numbers. map (function (item, index, array ){
Return (item * 2 );
})
Alert (mapResult); // [2, 4, 6, 8, 10, 8, 6, 4, 2]
// ForEach ()
Numbers. forEach (function (item, index, array ){
// No return value is returned when the operation is executed.
})
Merge methods: reduce (), reduceRight ()
ECMAScript5 provides methods to support browsers: IE9 +, Firefox 3 +, Safari 4 +, Opera 10.5 +, and Chrome
Both methods iterate the items of the array and construct a final returned value. The reduce () method starts from the first entry of the array, and the reduceRight () method starts from the end of the array.
The code is as follows:
Var values = [1, 2, 3, 4, 5];
Var sum = valuse. reduce (function (prev, cur, index, array ){
Prev + cur;
});
Alert (sum); // 15
The above is all the content of this article. I hope you will like it.