-2

i have array like this

var listShow = [1, 3]

and this

[
{
  id: 1,
  name: 'JOHN',
},
{
  id: 2,
  name: 'JOHNE',
},
{
  id: 3,
  name: 'JOHNW',
}
]

And I only want to return array if id value is same as above array, I want result like this

[
    {
      id: 1,
      name: 'JOHN',
    },
    {
      id: 3,
      name: 'JOHNW',
    }
]

Does anyone know how to make the above possible? I've tried using array.filter but it doesn't work

2
  • 1
    You say array.filter doesn't work. Please can you explain how it doesn't work? Perhaps show us the code you've tried? Commented Sep 8, 2022 at 8:46
  • 1
    dupe of dupes Commented Sep 8, 2022 at 8:48

1 Answer 1

-1
var a = [
{
  id: 1,
  name: 'JOHN',
},
{
  id: 2,
  name: 'JOHNE',
},
{
  id: 3,
  name: 'JOHNW',
}
]
var b = a.filter((item) => listShow.includes(item.id) )

console.log(b)
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.