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

时间:2022-09-03 17:17:52

数组去重

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 ]
 

原文:https://blog.csdn.net/momDIY/article/details/81351140