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
19.1k5 gold badges39 silver badges47 bronze badges
asked Aug 9, 2017 at 14:21
2

8 Answers 8

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)

answered Aug 9, 2017 at 14:32

Comments

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

answered Aug 9, 2017 at 14:26

3 Comments

looks complicated but gives the right result! thank you! +1
if two contains an item not in one does this still work?, sorry never mind re read the question and see he wants the ones in one
@Quince yes it will produce the same output.
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);

answered Aug 9, 2017 at 14:33

Comments

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

answered Aug 9, 2017 at 14:32

Comments

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

answered Aug 9, 2017 at 14:44

Comments

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

answered Aug 9, 2017 at 14:30

Comments

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

answered Aug 9, 2017 at 14:32

Comments

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.

answered Aug 9, 2017 at 14:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.