James
James

Reputation: 281

Use filter to return array where object value matches

I have 3 device tabs that should only display items that exist in that device. On each tab I have the device ID and an array of items that each contain the platform ID. I am trying to filter the array of items to only contain matching items.

const id = 1;

const items = [
    {
    id: 1
        title: "foo"
        device: 1
    },
  {
    id: 2
        title: "bar"
        device: 1
    },
  {
    id: 3
        title: "baz"
        device: 2
    }
]


expected = [
    {
    id: 1
        title: "foo"
        device: 1
    },
  {
    id: 2
        title: "bar"
        device: 1
    }
 ]

My current failed attempt:

offers.filter(key => {
    if (key.platformId === platform) {
      return;
    }
  });

Upvotes: 0

Views: 45

Answers (2)

user7720975
user7720975

Reputation:

Even though your code indentation and formatting seems a little bit weird, here you go.

const expected = items.filter((item) => item.device == id)

Upvotes: 1

Petr Lebedev
Petr Lebedev

Reputation: 36

const filteredArray = offers.filter((item) => item.device === id)

Upvotes: 0

Related Questions