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' }] //类数组
eg:数组
obj = {"1": 'value is 1', length: 2} a = Array.from(obj) //[undefined, "value is 1"]
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:prototype
arr1.fill(0,1,2) // ["1", 0, "3"]
Array.isArray() Array.isArray(arr1) //true
Array.prototype.filter() Array.prototype.map()
eg:code
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=6 arr1.reduce(reducer,4) //4+1+2+3=10
Array.prototype.some(callback) //执行callback函数,直到callback返回true var 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的第一个元素的索引。不然返回-1 function even(value){ return value % 2 === 0 } arr1.findIndex(even) // 1
Array.prototype.includes(searchElement) //是否包含seachElement arr1.includes(2) //true arr1.includes(0) //false
Array.prototype.pop() //删除数组的最后一个元素,并返回改元素的值 arr1.pop() // 3 arr1=[1,2]
Array.prototype.push() //增长元素到数组末尾,返回增长的元素 arr1.push(4) // 4 arr1=[1, 2, 3, 4]
Array.prototype.shift() //删除数组第一个元素,返回这个元素值 arr1.shift() // 1 arr1=[2,3]
Array.prototype.unshift() //增长元素到数组开头 ,返回数组长度 arr1.unshift(0) //4 arr1=[0,1,2,3]
Array.prototype.slice(start,end) //返回[start,end]浅拷贝到一个新数组,原数组不会被修改 arr1.slice(0,2) //[1,2] arr1=[1,2,3]
Array.prototype.splice() //经过删除现有元素或添加新元素来更改一个数组的内容,原数组会被修改 Array.prototype.slice(replace-index,replace-num,replace-value) //replace-num = 0 => inserts arr1.splice(1,0,0) //arr1=[1,0,2,3] //replace-num !== 0 => replace arr1.splice(1,1,4) //返回被替换的元素 =>2 arr1=[1,4,3]
Array.prototype.map(callback) arr1.map(val=>val*2) // [2, 4, 6]
Array.prototype.forEach(callback) arr1.forEach(elem =>{ console.log(elem) }) // 1 // 2 // 3
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"
Array.prototype.keys() //返回一个新的Array迭代器,它包含数组中每一个索引的键 for(let index of(arr1).keys()){ console.log(index) } // 0 // 1 // 2
Array.prototype.values() //返回一个新的Array迭代对象,包含数组每一个索引值 for(let elem of(arr1).values()){ console.log(elem) } // a // b // c