JS: remove duplicate values in array, including the original

后端 未结 4 1434
不知归路
不知归路 2020-12-04 02:44

Does anyone know how to remove duplicates in an array including the original value? I came across different snippets like this and this and some others but none in particul

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 03:34

    You could try filtering your array based on the number of occurrences of a specific value in that array:

    var arr = [1, 1, 2, 3, 5, 5]
    
    var res = arr.filter((el, _, arr) => {
          return arr.filter(el2 => el2 === el).length === 1
    })
    
    console.log(res)

提交回复
热议问题