xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

ES6 Set All In One

ES6 Set All In One

Set 集合

Map 字典/地图

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

Set

数组去重


const arr = [1, 2, 3, 3, 2, 1];

const unique = [...new Set(arr)];
// [1, 2, 3]

const arr = [1, 2, 3, 3, 2, 1];

// unique = arr.reduce((acc, item) => !acc.includes(item) ? acc.concat([item]) : acc, []);

const unique = arr.reduce((acc, item) => {
 if(!acc.includes(item)) {
   acc.push(item);
 }
 return acc;
}, []);

// [1, 2, 3]


refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-11-18 12:21  xgqfrms  阅读(107)  评论(0编辑  收藏  举报