DEV Community

Discussion on: JavaScript - remove duplicates from array

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Array of Objects.

import str from 'json-stable-stringify'

console.log(arrObjDupl.filter(
  (a, i, arr) => {
    const strA = str(a)
    return arr.findIndex((b) => str(b) === strA) === i
  }
))

// Or more efficiently
const tmpArr = arrObjDupliStr.map((a) => str(a))

console.log(arrObjDupl.filter(
  (_, i) => tmpArr.indexOf(tmpArr[i]) === i
))
Enter fullscreen mode Exit fullscreen mode

You can use YAML with sortKeys, if you mean ANY object.