0

I want to remove the duplicate values and leave only one occurrence of that value.

Example:

var myArray = ["blue","blue","red","red","green","blue","blue","red"];
//REMOVE DUPLICATES LEAVING ONE
//results in - myArray = ["blue","red","green"];
Dave Haigh
  • 4,369
  • 5
  • 34
  • 56

1 Answers1

0

You can filter duplicates like this (used ES6 syntax):

const myArray = ["blue","blue","red","red","green","blue","blue","red"];

const result = myArray.filter((a, i, arr) => arr.indexOf(a) === i);

console.log(result);
madox2
  • 49,493
  • 17
  • 99
  • 99