0

How can get all the items of array which do not exists another array?

I have one array with already selected values:

var selectedList = [1,2];

Another array with list of Objects like

var objList = [{Value:1, Name:'KL'},{Value:2, Name:'XYZ'},{Value:3, Name:'ABC'}];

I wanted result to be filter on value field of ObjList array and only those records come which do not exists in the selectedList.

Praveen Rawat
  • 638
  • 1
  • 11
  • 27

2 Answers2

1

You could use simple filter to do that

var selectedList = [1, 2];
var objList = [{
  Value: 1,
  Name: 'KL'
}, {
  Value: 2,
  Name: 'XYZ'
}, {
  Value: 3,
  Name: 'ABC'
}];

var data = objList.filter(x => selectedList.indexOf(x.Value) != -1)

console.log(data)
user93
  • 1,866
  • 5
  • 26
  • 45
0

You can try:

var bigArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var smallArray = ['b', 'c', 'g'];

var newArray = bigArray.filter( function(item) {
  return smallArray.indexOf(item) < 0;
});

or if using underscore.js use .difference()

Martin Shishkov
  • 2,709
  • 5
  • 27
  • 44