0

I have two array of objects.

e = [{uniqueId:'',active:'a',qId:10},{uniqueId:'',active:'a',qId:11}]
f = [{uniqueId:50,active:'a',qId:10},{uniqueId:51,active:'a',qId:11},{uniqueId:52,active:'a',qId:13}]

I want to compare these objects and my final result will be like

result = [{uniqueId:50,active:'a',qId:10},{uniqueId:51,active:'a',qId:11}]

I tried

let result = e.filter(o1 => f.some(o2 => o1.qId != o2.qId));

But am getting

[{uniqueId:50,active:'a',qId:10},{uniqueId:51,active:'a',qId:11},{uniqueId:52,active:'a',qId:13}]

How to achieve the desired output?

2
  • 2
    please add what you mean by "... values that are not equal" Commented Jul 31, 2018 at 7:28
  • Compare the two array of objects using 'qId', if qId is present in one object and not in the other, i don't want that object in result Commented Jul 31, 2018 at 7:30

4 Answers 4

2

It looks like you should be filtering f, not e, because the result shows values from f, not from e.

For the least complexity, turn the e array's qIds into a Set for quick lookup first. (Sets have O(1) lookup time, compared to O(N) complexity for .some)

const e = [{uniqueId:'',active:'a',qId:10},{uniqueId:'',active:'a',qId:11}]
const f = [{uniqueId:50,active:'a',qId:10},{uniqueId:51,active:'a',qId:11},{uniqueId:52,active:'a',qId:13}]

const qIds = new Set(e.map(({ qId }) => qId));
console.log(f.filter(({ qId }) => qIds.has(qId)));

Sign up to request clarification or add additional context in comments.

Comments

2

i hope you need to compare on qId..

let e = [{uniqueId:'',active:'a',qId:10},{uniqueId:'',active:'a',qId:11}]
let f = [{uniqueId:50,active:'a',qId:10},{uniqueId:51,active:'a',qId:11},{uniqueId:52,active:'a',qId:13}]
let res = [];
f.forEach(fo => {
  e.forEach(eo => {
    if(fo.qId === eo.qId){
       res.push(fo)
    }
  })
})

console.log(res)

Comments

1

You can use Array.filter() and Array.some() in combination to get that result:

e = [{uniqueId:'',active:'a',qId:10},{uniqueId:'',active:'a',qId:11}]
f = [{uniqueId:50,active:'a',qId:10},{uniqueId:51,active:'a',qId:11},{uniqueId:52,active:'a',qId:13}];
var res = f.filter(fItem => e.some(({qId}) => fItem.qId === qId));
console.log(res);

Comments

0

You could check if array e has the same qId value for filtering f.

var e = [{ uniqueId: '', active: 'a', qId: 10 }, { uniqueId: '', active: 'a', qId: 11 }],
    f = [{ uniqueId: 50, active: 'a', qId: 10 }, { uniqueId: 51, active: 'a', qId: 11 }, { uniqueId: 52, active: 'a', qId: 13 }],
    result = f.filter(({ qId }) => e.some(o => o.qId === qId));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

Hi @NinaScholz; i am doing the same thing with the just one condition change about not equal to !== but that is not working in my case. So is there any other method to work with the not equal to condition ? Check my live example over here: playcode.io/586815
@aananddham, please ask a new question, if necessary. the comment section is not to answer another question.

Your Answer

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.