js数组去重、获取重复数据以及获取非重复数据--一句话函数

数组去重

let arr = [11, 23, 26, 23, 11, 9]
const deduplication = arr => [...new Set(arr)]

console.log(deduplication(arr));  // [ 11, 23, 26, 9 ]

保留数组中非重复数据

let arr = [11, 23, 26, 23, 11, 9]
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i))

console.log(filterNonUnique(arr));  // [ 26, 9 ]

保留数组中重复元素

let arr = [11, 23, 26, 23, 11, 9]
const filterUnique = arr => arr.filter(i => arr.indexOf(i) !== arr.lastIndexOf(i))

console.log(filterUnique(arr)); // [ 11, 23, 23, 11 ]

你可能感兴趣的:(Javascript)