ES6 Set All In One_SetES6 Set All In One Set 集合



ES6 Set All In One

数组去重


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

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


ES6 Set All In One_Set

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]



ES6 Set All In One_Set_03