[Monitoring array]
- Use the instanceof operator to detect
ar arr = [1,2,3]; // arr = 'distinguish good and bad'; if(arr instanceof Array){ console.log('It's an array.'); }else{ console.log('Not an array'); }
Note: The premise of using instanceof operator in ES3 is that there is only one execution environment for the current page. When there are multiple frameworks in the page, this detection method has problems. To solve this problem, ES5 proposes a second method.
- Array.inArray() method
var arr = [1,2,3]; // arr = 'distinguish good and bad'; if(Array.isArray(arr)){ console.log('It's an array.'); }else{ console.log('Not an array'); }
Note: IE8 does not support this detection, of course, its compatibility is achievable; however, no one has been interested in IE8 for a long time now.
[Array Conversion String]
- toString() method
var arr = [ [1,2,3], ['distinguish good and bad','Coffee'], [8,9] ]; console.log(arr.toString());//1,2,3,distinguish good and bad,Coffee,8,9
- join() method
var arr = [ [1,2,3], ['distinguish good and bad','Coffee'], [8,9] ]; console.log(arr.join(','));//1,2,3,distinguish good and bad,Coffee,8,9 console.log(arr.join('||'));//1,2,3||distinguish good and bad,Coffee||8,9
[Array Sorting]
- reverse() Reverses the order of array items
var arr = [1,2,3,4]; var arr1 = ['distinguish good and bad','Gigi','stammering']; console.log(arr.reverse());//[4, 3, 2, 1] console.log(arr1.reverse());//["stammering", "Gigi", "distinguish good and bad"]
- sort()
- By default, array items are arranged in ascending order (the default comparison is the string form of each item, toString())
var arr = [1,2,3,4]; var arr1 = ['distinguish good and bad','Gigi','stammering']; var arr2 = [1,2,3,4,11]; console.log(arr.sort());//[1, 2, 3, 4] console.log(arr1.sort());// ["Gigi", "stammering", "distinguish good and bad"] console.log(arr2.sort());//[1, 11, 2, 3, 4]
- sort() accepts the parameters of a comparison function
function compare(value1, value2){ if(value1 < value2){ return -1; }else if(value1 > value2){ return 1; }else{ return 0; } } // Simplified comparison function function compare(value1, value2){ return value1 - value2; } var arr = [1,2,3,4,11]; console.log(arr.sort(compare));//[1, 2, 3, 4, 11]
- By default, array items are arranged in ascending order (the default comparison is the string form of each item, toString())
[Array insertion]
- push() inserts a new item from behind
var arr = [1,2,3,4]; arr.push(8,9) console.log(arr);//[1, 2, 3, 4, 8, 9]
- unshift() inserts a new item from the front
var arr = [1,2,3,4]; arr.unshift(8,9) console.log(arr);//[8, 9, 1, 2, 3, 4]
- splice() - insert
var arr = [1,2,3,4,5]; arr.splice(3,0,'distinguish good and bad') console.log(arr);//[1, 2, 3, "distinguish good and bad", 4, 5]
[Array deletion]
- pop() deletes the last item of the array
var arr = [1,2,3,4]; arr.pop() console.log(arr);//[1, 2, 3]
- shift() deletes the first item of the array
var arr = [1,2,3,4]; arr.shift() console.log(arr);//[2, 3, 4]
- splice() - Delete
var arr = [1,2,3,4,5,6,7,8,9]; arr.splice(0,3) console.log(arr);//[4, 5, 6, 7, 8, 9]
[Array substitution]
splice() - Replacement
var arr = [1,2,3,4,5]; arr.splice(2,1,'distinguish good and bad','Gigi') console.log(arr);//[1, 2, "distinguish good and bad", "Gigi", 4, 5]
[Array Location Finding]
var arr = [1,2,'distinguish good and bad',3,4,5,'distinguish good and bad','Gigi']; console.log(arr.indexOf('distinguish good and bad'));//2 console.log(arr.lastIndexOf('distinguish good and bad'));//6
[Combination of Numbers]
concat()
var arr = [1,2,3,4,11]; var arr1 = ['distinguish good and bad','Gigi']; console.log(arr.concat(arr1));// [1, 2, 3, 4, 11, "distinguish good and bad", "Gigi"] console.log(arr1.concat(arr));//["distinguish good and bad", "Gigi", 1, 2, 3, 4, 11]
[Array interception]
slice(), receives one or two parameters, the first parameter is the starting position of the array (the result includes the starting position item), the second parameter is the ending position (the result excludes the ending item); supports negative numbers, minus negative numbers by length length length length, that is, the position of the current item.
var arr = [1,2,3,4,5,6,7,8,9]; console.log(arr.slice(1));//[2, 3, 4, 5, 6, 7, 8, 9] console.log(arr.slice(1,5));//[2, 3, 4, 5] console.log(arr.slice(-3));//[7, 8, 9] console.log(arr.slice(-4,-1));//[6, 7, 8]
[Iterative method]
- every() runs a given function for each item in the array and returns true if the function returns true for each item.
var arr = [1,2,3,4,5,6,7,8]; var everyMethods = arr.every(function(item, index, array){ return item > 0; }); console.log(everyMethods);//true
- filter() runs a given function for each item in the array, returning an array of items that return true.
var arr = [1,2,3,4,5,6,7,8]; var filterMethods = arr.filter(function(item, index, array){ return item > 6; }); console.log(filterMethods);//[7, 8]
- forEach() runs the given function for each item in the array. This method does not return a value (the same as using for loop iteration arrays)
var arr = [1,2,3,4,5,6,7,8]; var forEachMethods = arr.forEach(function(item, index, array){ console.log('item=>'+item); console.log('index=>'+index); console.log('array=>'+array); }); console.log(forEachMethods); // item=>1 // array.html:24 index=>0 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>2 // array.html:24 index=>1 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>3 // array.html:24 index=>2 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>4 // array.html:24 index=>3 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>5 // array.html:24 index=>4 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>6 // array.html:24 index=>5 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>7 // array.html:24 index=>6 // array.html:25 array=>1,2,3,4,5,6,7,8 // array.html:23 item=>8 // array.html:24 index=>7 // array.html:25 array=>1,2,3,4,5,6,7,8
- map() runs a given function for each item in the array, returning an array of the results of each function call
var arr = [1,2,3,4,5,6,7,8]; var mapMethods = arr.map(function(item, index, array){ return item*2; }); console.log(mapMethods);//[2, 4, 6, 8, 10, 12, 14, 16]
- some() runs a given function for each item in the array, and returns true if the function returns true for any item.
var arr = [1,2,3,4,5,6,7,8]; var someMethods = arr.some(function(item, index, array){ return item > 6; }); console.log(someMethods);//true
[Merging Method]
reduce() method and reduce Right () method
These two methods are relatively simple to use at present. In the actual development, other ways can be used to achieve this effect, so keep them first, in case I understand that day, and then add them.