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

3
  • Have you tried anything? The lodash documentation is a good place to start
    – Andrew Li
    Commented Dec 1, 2017 at 13:31
  • Plain JS filter or reduce too
    – mplungjan
    Commented Dec 1, 2017 at 13:33
  • i tried with this _.uniqBy but i need to delete the duplicate value from the list
    – achu
    Commented Dec 1, 2017 at 13:37

3 Answers 3

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);

1
  • thank u for replay @Nina Scholz this want to happen in click function. if the value twice I want to delete it.
    – achu
    Commented Dec 1, 2017 at 13:48
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);

0

you could use _.uniqBy()

  _.uniqBy(list ,function(m){
         return  list.indexOf(m) === list.lastIndexOf(m)
    })

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.