3

Let's say I have:

let object1 = [{id: 15, name: name1}, {id: 0, name: name2}, {id: 98, name: name3}];
let object2 = [{id: 2, action: action}, {id: 88, action: action}, {id: 0, action: action}];

ID numbers don't match on purpose. How could I get name values from object1 whose ID's don't appear in object2?

Edit: I've tried to

let results;
for (let i = 0; i < object1.length; i++) {
         results = object2.filter(element => { return element.id != object1[i].id } );
    }

Also tried to get it working with .map(), but with no luck.

2
  • 2
    Your question sounds actually like you haven't try anything and expect a free code service. This is going to be really badly welcomed. If you've tried anything, please show us your attempts Commented Dec 2, 2021 at 20:16
  • 4
    Oh sorry about that. Let me go through something that I've tried so far! Editing the original post. Commented Dec 2, 2021 at 20:16

1 Answer 1

3

You could use filter() on one of the arrays and find() to filter out the objects from that array with ids that don't occur in the other array.

let oArr1 = [{id: 15, name: "name1"}, {id: 0, name: "name2"}, {id: 98, name: "name3"}];
let oArr2 = [{id: 2, action: "action"}, {id: 88, action: "action"}, {id: 0, action: "action"}];

const result = oArr1.filter(x => !oArr2.find(y => y.id === x.id));

console.log(result);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.