JS中的数组总结


1.迭代方法

方法返回值用法
entries数组的可迭代对象arr.antries().next().value 这样会获取到第一个
every全满足返回true,否则返回falsearr.every((item)=>{
return item ==‘tt’})
some有一个满足返回false,全不满足返回truearr.some(()=>{
return ‘222’})
filter返回满足条件的新数组arr.filter(()=>{
return ‘111’})
forEach()没有返回值,本质上是for循环,会执行里面的函数。array.forEach(function(currentValue, index, arr), thisValue)
map返回处理过函数的值组成的数组array.map(function(currentValue,index,arr), thisValue)

2.寻找方法

方法返回值用法
find返回符合条件的,后面不会再执行,没有返回undefinredarray.find(function(currentValue, index, arr),thisValue)
findIndex返回符合的下标值,没有返回-1array.findIndex(function(currentValue, index, arr), thisValue)
indexOf
(最开始出现的位置,从前往后找)
返回下标,没有返回-1array.indexOf(item,start)
lastIndexOf
(最后出现的位置,从后往前找)
返回下标,没有返回-1array.lastIndexOf(item,start)
includes包含返回true,不包含返回false[1, 2, 3].includes(3, -1);

3.转换方法

方法返回值用法
join返回一个字符串fruits.join(" and ")
toString返回一个字符串fruits.toString()
valueOf(不会显示调用)数组本身arr.valueOf()

4.操作方法

方法返回值用法
concat返回连接的数组arr.concat(arr2)
reduce返回最终结果array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
slice(
截取数组)
返回一个新数组,从start到end前面array.slice(start, end)

5. 栈堆方法 (都会改变原始数组)

方法返回值用法
push(后面加元素)新数组的长度array.push(item1, item2, …, itemX)
pop(后面删元素)删除的元素array.pop()
unshift(前面加元素)新数组的长度array.unshift(item1,item2, …, itemX)
shift(前面删元素)删除的的元素array.shift()
splice(规定位置的添加删除)如果仅删除一个元素,则返回一个元素的数组。 如果未删除任何元素,则返回空数组。array.splice(index,howmany,item1,…,itemX)
移除数组的第三个元素,并在数组第三个位置添加新元素:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1,"Lemon","Kiwi");
fruits 输出结果:

Banana,Orange,Lemon,Kiwi,Mango

sort排好序的数组points.sort(function(a,b){return a-b});
reverse反转的数组array.reverse()

6. ES6中新增的数组的方法

1.Array.of() 将所有值形成一个数组

console.log(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4]
 
// 参数值可为不同类型
console.log(Array.of(1, '2', true)); // [1, '2', true]
 
// 参数为空时返回空数组
console.log(Array.of()); // []

2.Array.from() 将类数组对象或可迭代对象转化为数组。

// 参数为数组,返回与原数组一样的数组
console.log(Array.from([1, 2])); // [1, 2]
 
// 参数含空位
console.log(Array.from([1, , 3])); // [1, undefined, 3]

3.

find() findIndex()

fill() copyWithin()

entries() keys() values() includes()

4.flat()

console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
 
// 指定转换的嵌套层数
console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]
 
// 不管嵌套多少层
console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
 
// 自动跳过空位
console.log([1, [2, , 3]].flat());<p> // [1, 2, 3]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值