一、添加数组
- push()在数组末尾添加
let a = ['1', '2', '3', '4', '5']
let b = a.push('7')
console.log(a)// ["1", "2", "3", "4", "5", "7"]
console.log(b)// 6 返回的是数组的长度
- unshift()在数组的头部添加
let a = ['1', '2', '3', '4', '5']
let b = a.unshift('7')
console.log(a)// ["7", "1", "2", "3", "4", "5"]
console.log(b)// 6 返回的是数组的长度
- splice(0,0,x)(开始删除位置的索引,删除的数组,原数组补位)----->unshift
let a = ['1', '2', '3', '4', '5']
let b = a.splice(0, 0, '7')
console.log(a)// ["7", "1", "2", "3", "4", "5"]
console.log(b)// [] 返回的删除的数组
- splice(n,m,x)(开始删除位置的索引,删除的数组,原数组补位)从n开始删除m个元素,把新增的元素X放在n的前面,把删除的元素当成一个新数组返回,原始数组会改变
let a = ['1', '2', '3', '4', '5']
let b = a.splice(1, 1, '7')
console.log(a)// ["1", "7", "3", "4", "5"]
console.log(b)// ["2"]
- splice(n,0,x)(开始删除位置的索引,删除的数组,原数组补位)从n开始删除0个元素,把新增加的元素x放在n的前面,返回的是一个空数组,原有数组改变
let a = ['1', '2', '3', '4', '5']
let b = a.splice(1, 0, '9')
console.log(a)// ["1", "9", "2", "3", "4", "5"]
console.log(b)// [] 返回的删除的数组
- splice(n,m)(开始删除位置的索引,删除的数组,原数组补位)从索引n开始删除m个元素,把删除的内容当做新数组返回,原有数组改变
let a = ['1', '2', '3', '4', '5']
let b = a.splice(0, 5)
console.log(a)// []
console.log(b)// ["1", "2", "3", "4", "5"] 返回的删除的数组
二、删除数组
- pop() 删除数组的末尾
let a = ['1', '2', '3', '4', '9']
let b = a.pop()
console.log(a)// ["1", "2", "3", "4"]
console.log(b)// 9 返回数组的最后一项
- shift()删除数组的头部
let a = ['8', '2', '3', '4', '9']
let b = a.shift()
console.log(a)// ["2", "3", "4", "9"]
console.log(b)// 8 返回数组的最后一项
- splice(a.length-1,1)删除最后一项
let a = ['8', '2', '3', '4', '9']
let b = a.splice(a.length - 1, 1)
console.log(a)// ["8", "2", "3", "4"]
console.log(b)// ["9"] 返回数组的最后一项的新数组
let a = ['8', '2', '3', '4', '9']
let b = a.splice(a.length - 1, 1)
console.log(a)// ["8", "2", "3", "4"]
let c = a.splice(a.length - 2, 2)
console.log(a)// ["8", "2"]
console.log(b)// ["9"] 返回数组的最后一项的新数组
console.log(c)// ["3", "4"]返回数组的删除项的新数组
三、数组的一些操作方法
- Array.from(Object,处理的结果放置处,绑定this)将一个对象转化成数组(Object需要有length属性才能转化成功,默认数组长度为0)第二个参数类似数组的map方法,第三个参数map函数中this指向的对象
let a = {
0: 'ling',
1: '22',
length: 2
}
let b = Array.from(a)
console.log(b) // ["ling", "22"]
let c = Array.from(a,v=>v||'')
console.log(c)//["ling", "22"]
将Set对象的元素转换成一个数组
let set = new Set()
set.add(1).add(2).add(3)
console.log(Array.from(set))//[1, 2, 3]
getArray () {
let a = Array.from([1, 2, 3, 4, 5], (x) => { return this.handle(x) })
console.log(a) //[3, 4, 5, 6, 7]
},
handle (n) {
return n + 2
}
- Array.isArray判断传递的值是否是一个数组
- Array.of把所有的参数放到一个数组中返回
console.log(Array.of(7)) // [7]
- Array.apply创建空数组(创建的数组带索引)
let a = Array.apply(null, {length: 2})
console.log(a) // [undefined, undefined]
let b = a.map(v => 1)
console.log(b) // [1, 1]
let a2 = new Array(2)
console.log(a2) // [empty × 2]
let b2 = a2.map(v => 1)
console.log(b2) // [empty × 2]
- concat合并多个数组并返回一个新数组(原有数组不变)
let a = ['1', '2']
let b = ['3', '4']
let c = a.concat(b)
console.log(c) // ["1", "2", "3", "4"]
- copyWithin(target,start,end)从数组的指定位置拷贝元素到另一个指定位置,选择数组的某个下标,从该位置开始复制数组元素,默认从0开始复制。也可以指定要复制的元素范围。
arr.copyWithin(target,start,end)
let a = ['1', '2', '3', '4', '5']
console.log(a.copyWithin(2)) // ["1", "2", "1", "2", "3"]
console.log(a.copyWithin(2, 1)) // ["1", "2", "2", "3", "4"]从下标为2的元素开始,复制数组,指定复制的第一个元素下标为1,所以3,4,5,被2,3,4替换掉了
console.log(a.copyWithin(2, 1, 2)) // ["1", "2", "2", "4", "5"]从下标为2的元素开始,复制数组,指定复制的第一个元素下标为1,结束位置为2,所以3被2替换掉了
- join()方法用于把数组中的所有元素放入一个字符串,元素是通过指定的分割符进行分割的,默认使用',’号分割,不改变原数组
let a = ['1', '2', '3', '4', '5']
console.log(a.join()) // 1,2,3,4,5
console.log(a)// ["1", "2", "3", "4", "5"]
- slice(start,end)返回一个新的数组,[start,end)的arrayObject中的元素。返回选定的元素,该方法不会修改原数组
let a = ['1', '2', '3', '4', '5']
console.log(a.slice(1, 3)) // ["2", "3"]
console.log(a)// ["1", "2", "3", "4", "5"]
- substring()和substr()如果只写一个参数两者的作用都一样,都是是截取字符串从当前下标以后直到字符串最后的字符串片段;不同点是:substr(startIndex,length)第二个参数是截取字符串的长度(从起始点截取某个长度的字符串);substring(startIndex,endIndex)第二个参数是截取字符串最终的下标[startIndex,endIndex)
let a = '123456789'
console.log(a.substr(2)) // 3456789
console.log(a.substring(2)) // 3456789
console.log(a.substr(2, 3)) // 345
console.log(a.substring(2, 3)) // 3
- sort()排序Unicode code位置排序,默认升序
let a = ['cherries', 'apples', 'bananas'];
console.log(a.sort()); // ['apples', 'bananas', 'cherries']
let b = [1, 10, 21, 2];
console.log(b.sort()); // [1, 10, 2, 21]
- reverse()方法用于颠倒数组中元素的顺序。返回的是颠倒后的数组,会改变原数组。
let a = [2,3,4];
console.log(a.reverse()); //[4, 3, 2]
console.log(a); //[4, 3, 2]
- indexOf和lastIndexOf都接受两个参数:查找的值,查找起始位置,不存在返回-1;存在返回位置。indexOf是从前往后查找,lastIndexOf是从后往前查找。
let a = [1, 2, 3, 4, 5, 3]
console.log(a.indexOf(2))// 1
console.log(a.indexOf(0))// -1
console.log(a.lastIndexOf(2))// 1
console.log(a.lastIndexOf(0))// -1
console.log(a.lastIndexOf(2, 3))// 1
console.log(a.lastIndexOf(2, 1))// 1
- every()对数组的每一项都运行给定的函数,每一项都返回true,则返回true,有一个不符合要求的就会返回false
Isevery (ele, index, arr) {
return ele < 10
},
let a = [1, 2, 3, 4, 5, 11]
let b = [1,2,3,4,5]
console.log(a.every(Isevery))//false
console.log(b.every(Isevery)//true
- some()对数组的每一项都运行给定的函数,任意一项返回true,则返回true
Isevery (ele, index, arr) {
return ele > 10
},
let a = [12, 1, 2, 3, 4, 5]
console.log(a.some(Isevery))//true
- filter()对数组的每一项都运行给定的函数,返回结果为true的项组成的数组
Isevery (ele, index, arr) {
return ele > 10
},
let a = [12, 1, 2, 3, 4, 5, 14, 15]
console.log(a.filter(Isevery))//[12, 14, 15]
- map()对数组的每一项都运行给定的函数,返回每次函数调用的结果组成一个新数组
let a = [1, 2, 3, 4, 5]
let doubles = a.map(function (x) {
return x * 2
})
console.log(doubles) // [2, 4, 6, 8, 10]
console.log(a) // [1, 2, 3, 4, 5]
- forEach()数组遍历
let a = [1, 2, 3, 4, 5]
let b = []
a.forEach(function (x) {
x = x * 2
b.push(x)
})
console.log(b) // [2, 4, 6, 8, 10]
console.log(a) // [1, 2, 3, 4, 5]
es6新增的一些操作数组的方法
- find()传入一个回调函数,找到数组中符合当前搜索规则的第一个元素,返回它,并且终止搜索
let a = [1, 2, 3, 4, 5]
let b = ''
b = a.find(x => x === 1)
console.log(b) // 1
console.log(a) // [1, 2, 3, 4, 5]
- findIndex传入一个回调函数,找到数组中符合当前搜索规则的第一个元素,返回它的下标,终止搜索
let a = [1, 2, 3, 4, 5]
let b = ' '
b = a.findIndex(x => x === 1)
console.log(b) // 0
console.log(a) // [1, 2, 3, 4, 5]
- fill()用新元素替换掉数组内的元素,可以指定替换下标范围
let a = [1, 2, 3, 4, 5]
let b = ' '
b = a.fill('5', 1, 3)
console.log(b) // [1, "5", "5", 4, 5]
console.log(a) // [1, "5", "5", 4, 5]
- entries()返回迭代器:返回键值对
let a = ['a', 'b', 'c']
for (let v of a.entries()) {
console.log(v)// [0, "a"] // [1, "b"]// [2, "c"]
}
// Set
let a = new Set(['a', 'b', 'c'])
for (let v of a.entries()) {
console.log(v) // ["a", "a"] // ["b", "b"] // ["c","c"]
}
// Map
let a = new Map()
a.set('a', 'a')
a.set('b', 'b')
for (let v of a.entries()) {
console.log(v) // ["a", "a"] //["b", "b"]
}
- values()返回迭代器:返回键值对的value
let a = ['a','b','c']
for(let v of a.values()){
console.log(v) // a b c
}
- keys()返回迭代器:返回键值对的key
let a = ['a','b','c']
for(let v of a.keys()){
console.log(v)// 0 1 2
}
// Set
let b = new Set(a);
for(let v of b.keys()){
console.log(v) // a b c
}
// Map
let c = new Map()
c.set('a','a')
c.set('b','b')
for(let v of c.keys()){
console.log(v) // a b
}
- includes()判断数组中是否存在该元素,参数:查找的值,起始位置,可以替换es5时代的indexOf判断方式。indexOf判断元素是否为NaN,会判断错误
let a = ['a','b','c']
let b = a.includes('c')
let c = a.includes('4')
console.log(b) // true
console.log(c) // false
本总结仅仅是为加强自己的对数组的理解和记忆,如果有哪里有错误,请及时反馈给我,大家一起来找茬呀!