3

Given two arrays: (one and two).

one: contains values

two: contains objects with values

I need to get the values from one which are not in two. I've tried with .filter() and .indexOf() but don't get the desired result.

In the following case I expect result to have the value 333. How to achieve that?

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

var result = two.filter(function (item) {
    console.log(item.identifier, one.indexOf(item.identifier));
});

console.log('result: ', result);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
caramba
  • 21,963
  • 19
  • 86
  • 127
  • 1
    Possible duplicate of [Filter Array Not in Another Array](https://stackoverflow.com/questions/33577868/filter-array-not-in-another-array) – Daniel Beck Aug 09 '17 at 14:23
  • 1
    Or https://stackoverflow.com/questions/2963281/javascript-algorithm-to-find-elements-in-array-that-are-not-in-another-array may be better – Daniel Beck Aug 09 '17 at 14:24

8 Answers8

2

I would extract the identifier values from two and then run the filter over one:

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

// get a flattened array of identifier values [111, 222, 444]
const identifiers = two.map((item) => item.identifier);

var result = one.filter(function (item) {
    console.log(item, identifiers.indexOf(item) === -1);
    return identifiers.indexOf(item) === -1;
});

console.log('result: ', result);
Dario
  • 6,152
  • 9
  • 39
  • 50
2

Just do what you want, filter one and take only those which are not in two (find it in two, if found it will return the object i.e. true equivalent if not found then it will return undefined i.e. false equivalent and negate ! it)

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];
var resultArr = one.filter(function(val){
    return !two.find(function(obj){
        return val===obj.identifier;
    });
});

console.log(resultArr)
Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32
2

You do not return a Boolean value from .filter().

You can iterate one array and can use .some() and ! operator to check if the current value exists at two array "identifier" property

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

var result = one.filter(function (item) {
    return !two.some(function(el) {
      return el.identifier === item
    })
});

console.log('result: ', result);
guest271314
  • 1
  • 15
  • 104
  • 177
1

You can filter the array one returning the elements that are not found in array two.

Code:

const one = [111, 222, 333, 444];
const two = [{identifier: 111},{identifier: 222},{identifier: 444}];
const result = one.filter(oneElem => !two.find(twoElem => oneElem === twoElem.identifier));

console.log('result: ', result);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1

You could use a Set and filter the values of one.

var one = [111, 222, 333, 444],
    two = [{ identifier: 111 }, { identifier: 222 }, { identifier: 444 } ],
    result = one.filter((s => a => !s.has(a))(new Set(two.map(o => o.identifier))));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can map the variable two first

var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];
two = two.map(function(item) {
  return item.identifier;
});

var result = one.filter(function (item) {
    return two.indexOf(item) == -1;
});

console.log('result: ', result);
MrChrissss
  • 271
  • 1
  • 2
  • 11
0

You can create a temporary array with all the values from array two. Then use indexOf to check if any value from array one is missin in array two

var one = [111, 222, 333, 444];
var two = [{
    identifier: 111
  },
  {
    identifier: 222
  },
  {
    identifier: 444
  }
];
var tempArray = []
two.forEach(function(item) {
  tempArray.push(item.identifier)

})
one.forEach(function(item) {
  if (tempArray.indexOf(item) === -1) {
    console.log(item)
  }
})
brk
  • 48,835
  • 10
  • 56
  • 78
0
var one = [111, 222, 333, 444];
var two = [
  { identifier: 111 },
  { identifier: 222 },
  { identifier: 444 }
];

vals = two.map(e=>{ return e['identifier']})

val = one.filter(e => { return vals.indexOf(e) == -1})
console.log(val)

map values into array and then filter the first array.

not_python
  • 904
  • 6
  • 13