1

How do you remove duplicates entirely to have the result be: [2, 3]

var numbers = [1, 1, 2, 3];
var answer = numbers.filter(function(value, index){ return numbers.indexOf(value) == index });

console.log(answer);

The current result is [1, 2, 3]

  • Duplicate of [Remove duplicate values from JS array](https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array) – MrUpsidown Mar 19 '20 at 20:42
  • Or https://stackoverflow.com/questions/40715503/js-remove-duplicate-values-in-array-including-the-original – MrUpsidown Mar 19 '20 at 20:50

2 Answers2

3

You need to check with indexOf and lastIndexOf.

var numbers = [1, 1, 2, 3],
    answer = numbers.filter((v, _, a) => a.indexOf(v) === a.lastIndexOf(v));

console.log(answer);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
var uniq = []
var array= [1, 1, 2, 3],
var arrFiltered = array.filter(obj => !uniq[obj] && (uniq[obj] = true));
console.log('Filtered Array:', arrFiltered)
Mr Khan
  • 2,139
  • 1
  • 7
  • 22
  • 3
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – David Buck Mar 20 '20 at 13:30