Array数组操作

199 阅读1分钟

对原数组进行操作

Array.prototype.reverse()

myArray.reverse();

Array.prototype.shift()

myArray.shift();

Array.prototype.unshift()

myArray.unshift(element1, ..., elementN);

Array.prototype.splice()

myArray.splice(start, deleteCount, item1, item2, ...);

String.prototype.split()

var newArray=str.split([separator[, limit]]);

返回新数组,不改变原数组

Array.prototype.slice()

var newArray=array1.slice(begin, end);

Array.prototype.concat()

var newArray=array1.concat(array2);      //array1在前

Array.prototype.filter()

var newArray=array1.filter(value => value >= 10);  

Array.prototype.map()

var newArray = array1.map( x => x ** 2)

Array.prototype.reduce()

var number=array1.reduce((accumulator, currentValue) => accumulator + currentValue)

Array.prototype.join()

var string=array1.join();

Array.prototype.reduce()

var number=array1.reduce((accumulator, currentValue) => accumulator + currentValue);

Array.prototype.indexOf()

var num=array1.indexOf(searchElement[, fromIndex = 0])  //没有找到返回-1,找到返回索引

mozilla