DEV Community

Dezina
Dezina

Posted on • Updated on

Get unique values in javascript array & object array

Unique elements from array

     function onlyUnique(value, index, self) {
        return self.indexOf(value) === index;
      }

      var optionsArray = ['a', 1, 'a', 2, '1'];
      var unique = optionsArray.filter(onlyUnique);

      console.log("unique", unique);
Enter fullscreen mode Exit fullscreen mode

Unique objects from object array

    console.log("optionsArray", optionsArray)
    var unique = Array.from(new 
    Set(optionsArray.map(JSON.stringify))).map(JSON.parse);

    console.log("unique", unique)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)