Determine whether an object is an array: instanceof, Array.isArray()
For a web page or a global scope, use the instanceof operator.
if(value instance of Array){ //Judge whether the value is an array
}
instanceof operator It assumes that there is only one global execution environment, and if the web page contains multiple frameworks, use the new Array.isArray() method added by ECMAScript5.
if(Array.isArray(value)){//Judge whether the 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 the array in a browser that does not implement this method, use:
if(Object.prototype.toString.call(value)=="[object Array]"){
}
Convert arrays to strings: toLocaleString(), toString(), valueOf(), join()
The code copy 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);//a,b,c calls toString() method by default
alert(test.join(','));//a,b,c
alert(test.join('|'));//a|b|c
Add and remove array elements methods: push(), pop(), unshift(), shift()
The push() method can accept any number of parameters, add them one by one to the end of the array, and return the length of the array modified.
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 end of the array and returns the new array length.
The shift() method can remove the first item in the array and return the removed item.
The code copy 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
Sort methods: reverse() and sort()
The reverse() method will invert the array item line and operate the array itself.
The sort() method arranges array items in ascending order by default, and operates on the array itself.
The code copy 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);//0,1,10,15,5 The sort() method will call the toString() method of each array item, compare the strings to determine the sort. So the sort here is string sort
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, and a positive number if the two parameters are equal.
The code copy is as follows:
function compare(value1,value2){
if(value1<value2){
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 methods: concat(), slice(), splice()
The concat() method is used to concatenate two or more arrays. This method does not change the existing array, but only returns a copy of the array being joined. Returns a new array.
The code copy is as follows:
var a = [1,2,3];
alert(a.concat(4,5));//1,2,3,4,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 an arrayObject from start to end (excluding this element).
The code copy 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 items to/from the array and returns the deleted item. The array itself.
The first parameter: the starting position, the second parameter: the number of intercepted, the third parameter: the new element added.
The code copy is as follows:
//delete
var test=['a','b','c'];
var removed=test.splice(0,1)//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(1,0,'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(1,1,'d','e')//Insert d,e from position 1
alert(test3);//a,d,e,c
alert(removed3)//b
Position method: indexOf(), lastIndexOf()
ECMAScript5 provides methods to support browsers: IE9+, Firefox 2+, Safari 3+, Opera 9.5+, Chrome
The indexOf() method returns the first occurrence of a specified string value in the string.
The lastIndexOf() method returns the last position where the specified string value appears, searching from back to front at the specified position in a string.
When one parameter: represents the value to be found, returns the index position (starting from 0), and when two parameters: the first parameter represents the starting position, and the second parameter represents the value to be found.
The code copy 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(), some()
ECMAScript5 provides methods to support browsers: IE9+, Firefox 2+, Safari 3+, Opera 9.5+, Chrome
every(): Run a given function on each item in the array, and if the function returns true for each item, it returns true.
filter(): Run a given function on each item in the array, and returning an array of items that will return true.
forEach(): Runs a given function on each item in the array, and this method does not return a value.
map(): Run a given function on each item in the array, returning an array composed of the results of each function call.
some(): Runs a given function on each item in the array, and if the function returns true for either item, it returns true.
None of the above functions will modify the values contained in the array.
The code copy 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){
//There is no return value when executing the operation
})
Merge methods: reduce(), reduceRight()
ECMAScript5 provides methods to support browsers: IE9+, Firefox 3+, Safari 4+, Opera 10.5+, Chrome
Both methods iterate over the so-called array and then build a final returned value. The reduce() method starts from the first item of the array, and the reduceRight() method starts from the last item of the array.
The code copy is as follows:
var values=[1,2,3,4,5];
var sum=value.reduce(function(prev,cur,index,array){
prev+cur;
});
alert(sum);//15
The above is all about this article, I hope you like it.