es6 常用

查找元素是否存在,存在就删除不存在就添加
this.collapseList.includes(value) ?  
this.collapseList =  this.collapseList.filter(item => item !== value) :  this.collapseList.push(value);

const index = this.showMenuCheckGroupList.findIndex(item => item == value);
index > -1 ? this.showMenuCheckGroupList.splice(index, 1) : this.showMenuCheckGroupList.push(value);

数组去重
var arr = [1, 1, 2, 2, 3, 4, 5, 5, 4, 3, 2, 1]  //输出 [1,2,3,4,5]
Array.from(new Set(arr))  

var arr = [1, 1, 2, 2, 3, 4, 5, 5, 4, 3, 2, 1]  //输出 [1,2,3,4,5]
[...new Set(arr)]  


js中遍历数组并不会改变原始数组的方法总共有12:
ES5:
forEach、every 、some、 filter、map、reduce、reduceRight
ES6:
find、findIndex、keys、values、entries
 
const index = Array.findIndex((item) => item[key] == value);
 
 
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1
 
[1, 4, -5, 10].find((n) => n < 0)
// -5
 
[1, 5, 10, 15].find((value, index, arr)=> {
  return value > 9;
}) // 10
 
[1, 5, 10, 15].findIndex((value, index, arr)=> {
  return value > 9;
}) // 2
 
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[NaN].indexOf(NaN)
// -1
 
 
[1, 2, [3, 4]].flat()
// [1, 2, 3, 4]
 
[1, 2, [3, [4, 5]]].flat()
// [1, 2, 3, [4, 5]]
 
[1, 2, [3, [4, 5]]].flat(2)
// [1, 2, 3, 4, 5]
 
const arr = [5, 12, 8, 130, 44];
arr.at(2) // 8
arr.at(-2) // 130
 
/ forEach方法
Array.forEach((x,i) => console.log(i)); // 1
 
// filter方法
Array.filter(x => true) // ['a','b']
 
// every方法
Array.every(x => x==='a') // true
 
// reduce方法
Array.reduce((x,y) => x+y) // 3
 
// some方法
Array.some(x => x !== 'a') // false
 
// map方法
Array.map(x => 1) // [,1]
 
copyWithin()会连空位一起拷贝。
Array.copyWithin(2,0) // [,"a",,"a"]
 
// entries()
Array.entries()
 
// keys()
Array.keys()
 
// values()
Array.values()
 
// find()
Array.find(x => true)
 
// findIndex()
Array.findIndex(x => true) // 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ob杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值