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

140
Views
Find a value in array of objects
  const objectTest = {
    Shoes: [
      { id: 1, name: "xxx", image: "xxxxxx" },
      { id: 2, name: "yyy", image: "yyyyyy" },
    ],
    Top: [
      { id: 1, name: "zzz", image: "zzzzzz" },
      { id: 2, name: "aaa", image: "aaaaaa" },
    ],
  };

I know i can use the find method.

 const item = objectTest.Shoes.find((p) => p.name === "xxx");
 console.log(item);  ///{id: 1, name: 'xxx', image: 'xxxxxx'}

but what if i don't know "xxx" belongs to the Shoes group. How do I find "xxx" and return { id: 1, name: "xxx", image: "xxxxxx" }

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

0

Easiest way is to flatten the object values first:

Object.values(objectTest).flat()

then you can find it normally:

const item = Object.values(objectTest).flat().find((p) => p.name === "xxx");

If you want to know which "group" it belonged to, you must iterate over Object.entries(objectTest).

about 4 years ago · Juan Pablo Isaza Report

0

If the schema's of Shoes and Top are the same, you can flatten them like

  const objectTest = {
    Shoes: [
      { id: 1, name: "xxx", image: "xxxxxx" },
      { id: 2, name: "yyy", image: "yyyyyy" },
    ],
    Top: [
      { id: 1, name: "zzz", image: "zzzzzz" },
      { id: 2, name: "aaa", image: "aaaaaa" },
    ],
  };

const findX = (objectTest, name) => {
   return [...objectTest.Shoes, ...objectShoes.Top].find((p) => p.name === "xxx");
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use for-in loop to traverse array, then can find into particular objects array.

const objectTest = {
    Shoes: [
      { id: 1, name: "xxx", image: "xxxxxx" },
      { id: 2, name: "yyy", image: "yyyyyy" },
    ],
    Top: [
      { id: 1, name: "zzz", image: "zzzzzz" },
      { id: 2, name: "aaa", image: "aaaaaa" },
    ],
  };
  
  let obj;
  for(let prop in objectTest) {
    obj = objectTest[prop].find(x=>x.name == "xxx");
    if(obj)
        break;    
  }
  
  console.log(obj)

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!