Map () Map
function map(){ var arr = [1,2,3,4,5]; arr.map(function(val,index){ console.log(val * 2); })}//map(); //2 4 6 8 10
Filter () Filtering
function filter(){ var arr = [1,2,3,4,5,6,7]; var arr1 = arr.filter(function(val,index){ return val < 4 ; }) console.log( arr1 ); //[1,2,3] var arr2 = arr.filter(function(val,index){ //定义一个变量名接一下 return index < 3; }) console.log( arr2 ); //[1,2,3]}//filter();
Known an array [' A ', ' B ', ' C ', ' d ', ' e ', ' f '], using at least three methods, remove the third-digit element from the array, and get a new array of [' A ', ' B ', ' C ']. */
//var arr = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘]//var arr1 = arr.filter(function(val,index){ //return index < 3;//})//console.log(arr1)
Push () adds one or more elements to the end of the array and returns the new length
function push(){ var arr = [1,2,3]; arr.push(2,3,4); console.log(arr);}//push(); //[1,2,3,2,3,4]function push2(){ var arr1 = [1,2,3]; var arr2 = [4,5,6]; arr1.push( arr2 ); return arr1;}console.log( push2() ) //[ 1,2,3,[4,5,6] ]
Unshift () adds one or more elements to the beginning of the array and returns the new length
function unshift(){ var arr=[1,2,3]; arr.unshift(4,5,6); console.log(arr); //[4, 5, 6, 1, 2, 3]}//unshift();
Pop () deletes the last element of the array and returns the value of the deleted element
function pop(){ var arr = [1,2,3]; arr.pop(); console.log(arr); //[1,2]}//pop();
Shift () deletes the first value of the array and returns the value of the deleted element
function shift(){ var arr = [1,23,4]; arr.shift(); console.log(arr); //[23,4]}//shift();
The concat (3,4) method is used to connect two or more arrays
function concat(){ var arr1 = [1,2,3]; var arr2 = [4,5,6]; console.log( arr1.concat(arr2) ) //[1, 2, 3, 4, 5, 6] console.log( arr1.concat(1,2,3)) //[1, 2, 3, 1, 2, 3]}//concat();
Reverse () reverse the array
function reverse(){ var arr = [1,2,3,4,5,6,7,8]; console.log( arr.reverse() ) //[8, 7, 6, 5, 4, 3, 2, 1]}//reverse();
The---Splice () method is different from the slice () method, and the splice () method modifies the group directly slice (start,end) returns a new array that consists of the entries from the original array that specify the starting subscript to the end subscript
function slice(){ var arr = [1,2,3,4,5,6]; console.log( arr.slice(0,4) ); //[1, 2, 3, 4] }slice();
The splice () method adds/deletes an item to/from the array and then returns the item that was deleted
function splice(){ var arr = [1,2,3,4,5,6,7]; console.log( arr.splice(0,3) ); //[1,2,3]}//splice();
Join (separator) sets the element group of the array to a string, separator as a delimiter, and the default with a comma delimiter if omitted
function join(){ var arr = [1,2,3,4,5,6,7]; return arr.join("|");}//console.log( join() ) //"1|2|3|4|5|6|7"function join2(){ var arr = [1,2,3,4,5]; arr.join(); return arr.join().slice(0,3);}//console.log( join2() ) //1,2
Common ways to manipulate arrays