Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
JS filtering an Array item through a value in a nested Array

I can't figure out how to properly use .filter() to find an object in an Array by searching for a value in its nested Array.

I have the following data:

const orders = [
  {
    id: 1,
    items: [
      {
        itemId: "abc",
      },
      {
        itemId: "def",
      },
    ],
  },
  {
    id: 2,
    items: [
      {
        itemId: "jkl",
      },
      {
        itemId: "mno",
      },
    ],
  },
  {
    id: 3,
    items: [
      {
        itemId: "abc",
      },
      {
        itemId: "xyz",
      },
    ],
  },
];

I have the needed itemId: "abc" which I need to use to find all the objects that have items that also have the Id of "abc", so that the result is this:

 [
  {
    id: 1,
    items: [
      {
        itemId: "abc",
      },
      {
        itemId: "def",
      },
    ],
  },
  {
    id: 3,
    items: [
      {
        itemId: "abc",
      },
      {
        itemId: "xyz",
      },
    ],
  },
 ]

So far, I've tried the following:

orders &&
  orders.filter((order) => {
    return order.items.filter((item) => item.itemId === "abc");
  });

But it doesn't seem to work. What am I missing here?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Chris G beat me to it in the comments but he is right, you need to use order.items.some in your inner function:

const orders = [{
    id: 1,
    items: [{
        itemId: "abc",
      },
      {
        itemId: "def",
      },
    ],
  },
  {
    id: 2,
    items: [{
        itemId: "jkl",
      },
      {
        itemId: "mno",
      },
    ],
  },
  {
    id: 3,
    items: [{
        itemId: "abc",
      },
      {
        itemId: "xyz",
      },
    ],
  },
]

var ans = orders.filter((order) => {
  return order.items.some((item) => item.itemId === "abc");
});

console.log(ans)

about 4 years ago · Juan Pablo Isaza Report

0

const orders = [{
    id: 1,
    items: [{
        itemId: "abc",
      },
      {
        itemId: "def",
      },
    ],
  },
  {
    id: 2,
    items: [{
        itemId: "jkl",
      },
      {
        itemId: "mno",
      },
    ],
  },
  {
    id: 3,
    items: [{
        itemId: "abc",
      },
      {
        itemId: "xyz",
      },
    ],
  },
];

const result = orders.filter(order =>
  order.items.find(item => item.itemId === 'abc') !== undefined
)

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!