Array操作方法
创建数组
var arr=[]var arr = new Array()var arr1 = ["1", "2", "3"]var arr2 = ["a", "b", "c"]var arrLike = [{ name: '123', age: '18'}, { name: 'abc', age: '12'}] //类数组
- 将类数组转换真正的数组Array.from(array)
eg:
Array.from(arrLike)
修改数组
- 合并数组,返回新数组,原数组不变
Array.prototype.concat(arr1, arr2) //["1", "2", "3", "a", "b", "c"]arr1.concat(arr2) //["1", "2", "3", "a", "b", "c"]
- 数组转化为字符串
Array.prototype.join(separator) //以separator(默认为逗号)拼接为字符串。Array.prototype.toString() //把数组转换为字符串, 数组中的元素之间用逗号分隔。
eg:
arr1.join() //"1,2,3"arr1.join('') //"123"arr1.join('/') //"1/2/3"arr1.toString() //"1,2,3"
填充
Array.prototype.fill(value,start,end)//value 为填充值//start 为填充数组元素起点坐标//end 为填充数组元素终点坐标
eg:
arr1.fill(0,1,2) // ["1", 0, "3"]
判断
Array.isArray()Array.isArray(arr1) //true
筛选
Array.prototype.filter()Array.prototype.map()
eg:
var words = [ { id: '1', name: '123' }, { id: '2', name: 'abc' }]words.filter(d => d.id === '1') //[{id: "1" , name: "123"}] 返回结果为true的值arr1.map(x => x * 2) // [2,4,6] 返回所有项
排序
Array.prototype.reverse() //位置颠倒Array.prototype.sort()
eg:
arr1.reverse() //["3", 2, "1"]arr1.sort() //["1", "2", "3"]
递归
Array.prototype.reduce()
eg:
const reducer = (accumulation,currentValue)=>{ accumulation + currentValue}arr1.reduce(reducer) // 1+2+3=6arr1.reduce(reducer,4) //4+1+2+3=10
查找
Array.prototype.some(callback) //执行callback函数,直到callback返回truevar even = function(element){ return element % 2 === 0}arr1.some(even) //true
Array.prototype.every(callback) //数组的所有元素是否都通过callback函数function even(currentValue){ return currentValue < 5}arr1.every(even)
Array.prototype.find(callback) //在数组中返回符合callback第一个元素的值function even(value){ return even % 2 === 0}arr1.find(even) // 2
Array.prototype.findIndex(callback) //返回数组中满足callback的第一个元素的索引。否则返回-1function even(value){ return value % 2 === 0}arr1.findIndex(even) // 1
Array.prototype.includes(searchElement) //是否包含seachElementarr1.includes(2) //truearr1.includes(0) //false
增删
- pop()
Array.prototype.pop() //删除数组的最后一个元素,并返回改元素的值arr1.pop() // 3 arr1=[1,2]
- push()
Array.prototype.push() //增加元素到数组末尾,返回增加的元素arr1.push(4) // 4 arr1=[1, 2, 3, 4]
- shift()
Array.prototype.shift() //删除数组第一个元素,返回这个元素值arr1.shift() // 1 arr1=[2,3]
- unshift()
Array.prototype.unshift() //增加元素到数组开头 ,返回数组长度 arr1.unshift(0) //4 arr1=[0,1,2,3]
- slice()
Array.prototype.slice(start,end) //返回[start,end]浅拷贝到一个新数组,原数组不会被修改arr1.slice(0,2) //[1,2] arr1=[1,2,3]
- splice()
Array.prototype.splice() //通过删除现有元素或添加新元素来更改一个数组的内容,原数组会被修改Array.prototype.slice(replace-index,replace-num,replace-value)//replace-num = 0 => insertsarr1.splice(1,0,0) //arr1=[1,0,2,3]//replace-num !== 0 => replacearr1.splice(1,1,4) //返回被替换的元素 =>2 arr1=[1,4,3]
循环遍历
- map()
Array.prototype.map(callback)arr1.map(val=>val*2) // [2, 4, 6]
- forEach()
Array.prototype.forEach(callback)arr1.forEach(elem =>{ console.log(elem)})// 1// 2// 3
- entries()
Array.prototype.entries() //返回一个新的Array Iterator对象,该对象包含数组中每个索引的键/值对arr1 = ['a', 'b', 'c']for( let [index,elem] of (arr1).entries()){ console.log(index,elem)}// 0 "a"// 1 "b"// 2 "c"
- keys()
Array.prototype.keys() //返回一个新的Array迭代器,它包含数组中每个索引的键for(let index of(arr1).keys()){ console.log(index)}// 0// 1// 2
- values()
Array.prototype.values() //返回一个新的Array迭代对象,包含数组每个索引值for(let elem of(arr1).values()){ console.log(elem)}// a// b// c