Determine whether an object is an array: instanceof, Array.isArray()
For a web page or a global scope, you can use the instanceof operator.
if(value instanceof Array){ //Determine whether value is an array
}
The instanceof operator assumes that there is only one global execution environment. If the web page contains multiple frames, the new Array.isArray() method of ECMAScript5 is used.
if(Array.isArray(value)){//Determine whether value is an array
}
The browsers supported by the Array.isArray() method include IE9, Firefor 4, Safari5, Opera 10.5, and Chrome.
If you want to check an array in a browser that does not implement this method, use:
if(Object.prototype.toString.call(value)=="[object Array]"){
}
Convert array to string: toLocaleString(), toString(), valueOf(), join()
Methods for adding and removing 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 array length.
The pop() method removes the last item from the end of the array and returns the removed item.
The unshift() method adds any number of parameters to the front of the array and returns the new array length.
The shift() method removes the first item in the array and returns the removed item.
Sort methods: reverse() and sort()
The reverse() method will reverse the order of the array terms and operate on the array itself.
The sort() method sorts the array items in ascending order by default and operates on the array itself.
The sort() method can also pass in a comparison function.
The comparison function returns a negative number if the first parameter should be before the second, 0 if the two parameters are equal, and a positive number if the first parameter should be after the second.
Operation methods: concat(), slice(), splice()
The concat() method is used to concatenate two or more arrays. This method does not modify the existing array, but simply returns a copy of the concatenated array. Return a new array.
The slice() method returns selected elements from an existing array. Returns a new array containing the elements in arrayObject from start to end (exclusive).
The splice() method adds/removes items to/from an array and returns the removed item. Operate on the array itself.
The first parameter: the starting position, the second parameter: the number of interceptions, the third parameter: the new element to be appended.
Position methods: indexOf(), lastIndexOf()
ECMAScript5 provides methods to support browsers: IE9, Firefox 2, Safari 3, Opera 9.5, Chrome
The indexOf() method returns the position of the first occurrence of a specified string value in the string.
lastIndexOf() method can return the last occurrence position of a specified string value, searching from back to front at the specified position in a string.
When there is one parameter: it represents the value to be found, and the index position is returned (starting from 0). When there are two parameters: the first parameter represents the starting position, and the second parameter represents the value to be found.
alert(numbers.IndexOf(4,4));//5
alert(numbers.lastIndexOf(4,4));//3
Iteration methods: every(), filter(), forEach(), map(), some()
ECMAScript5 provides methods to support browsers: IE9, Firefox 2, Safari 3, Opera 9.5, Chrome
every(): Run the given function on each item in the array, and return true if the function returns true for each item.
filter(): Runs the given function on each item in the array and returns an array of items that the function will return true.
forEach(): Runs the given function on each item in the array. This method has no return value.
map(): Runs the given function on each item in the array and returns an array composed of the results of each function call.
some(): Runs the given function on each item in the array and returns true if the function returns true for any item.
None of the above functions will modify the values contained in the array.
//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){
//Perform operation without return value
})
Merge methods: reduce(), reduceRight()
ECMAScript5 provides methods to support browsers: IE9, Firefox 3, Safari 4, Opera 10.5, Chrome
Both methods iterate over all items of the array and then construct a final returned value. The reduce() method starts from the first item in the array, and the reduceRight() method starts from the end of the array.
The above is the entire content of this article, I hope you all like it.