0

how to delete the duplicate value from the array.

var list =[1,1,5,5,4,9]

my result will be

var list =[4,9]

how can I do by using lodash

achu
  • 639
  • 2
  • 10
  • 26

3 Answers3

3

You could check the index and last index of the actual value.

var list = [1, 1, 5, 5, 4, 9],
    result = list.filter((v, _, a) => a.indexOf(v) === a.lastIndexOf(v));
    
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • thank u for replay @Nina Scholz this want to happen in click function. if the value twice I want to delete it. – achu Dec 01 '17 at 13:48
0

you could use _.uniqBy()

  _.uniqBy(list ,function(m){
         return  list.indexOf(m) === list.lastIndexOf(m)
    })
MBehtemam
  • 7,865
  • 15
  • 66
  • 108
0

You can do

var list =[1,1,5,5,4,9];

let result = list.reduce((a, b) =>{
  a[b] = a[b] || 0;
  a[b]++;
  return a;
}, []).map((e, idx) => e==1? idx: undefined).filter(e => e);

console.log(result);
marvel308
  • 10,288
  • 1
  • 21
  • 32