0

I have 2 Array:

const arr1 = [
  {
    id: 1,
    name: "a"
  },
  {
    id: 2,
    name: "ab"
  },
  {
    id: 3,
    name: "abc"
}]

and

const arr2 = [{id:"1"}, {id:"3"}]

How can i get from two above array to get the result like that:

const result = ["a", "abc"]

I'm struggling with array built-in function. Thank you for reading.

1
  • 1
    why do you have mixed values like strings and numbers for id? have you tried anything? Commented Jun 12, 2020 at 15:51

1 Answer 1

1

You could do something like the following.

const arr1 = [{ id: 1, name: "a"}, {id: 2, name: "ab"}, { id: 3, name: "abc" }]
const arr2 = [{ id: 1 }, { id: 3 }];

const ids = arr2.map(item => item.id);
const includedIds = arr1.filter(item => ids.includes(item.id)).map(item => item.id)

console.log(includedIds)

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.