1、假如数组元素不是对象,如
let arr = [1, 2, 3, 2, 'a', 'b', 'a'] let set = new Set(data) let newArr = [...set] // 数组去重 console.log(newArr)
2、假如数组元素是对象,如
// 数组中的对象 去掉value相同的对象 let arr = [{value: 1, time:111,}, {value: 2, time: 222}, {value: 3, time: 333}, {value: 2, time: 222}] let newArr = arr.reduce((pre, cur) => { // console.log('pre', pre, 'cur', cur) let index = pre.findIndex((item) => { return item.value === cur.value }) index === -1 && pre.push(cur) return pre }, [])
console.log(newArr)