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

178
Views
How to get an object from one key,value in array

I have the following code which returns whether the key and value are found in an array object.

function iterate(array, prop, val) {
    Object.keys(array).forEach((key) => {
        if (key === prop && val === array[key]) {
            console.log(`key: ${key}, value: ${array[key]}`);
        }

        if (typeof array[key] === 'object') {
            iterate(array[key], prop, val);
        }
    });
}

That's ok but now I would like to access the full content of that specific object where those properties and values were found. This is my JSON:

[
   {
      "page":"main",
      "content":[
         {
            "type":"item",
            "item":[
               {
                  "name":"HAMMER",
                  "price":"1000"
               },
               {
                  "name":"SWORD",
                  "price":"2000"
               }
            ]
         }
      ]
   }
]

This is what I look for/get with my function:

"name":"HAMMER"

And this is what I need:

{
   "name":"HAMMER",
   "price":"1000"
},

Is there any way to achieve that or am I addressing this problem from the wrong perspective?

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

0

You could use a basic loop, to find an item from deep nested object...

let data = [{ "page": "main", "content": [{ "type": "item", "item": [{ "name": "HAMMER", "price": "1000" }, { "name": "SWORD", "price": "2000" }] }] }]
let [prop, value] = ["name", "HAMMER"]

loop: for (let page of data) {
    for (let type of page.content) {
        let found = type.item.find(v => v[prop] === value);
        if (found) {
            console.log(found)
            break loop;
        }
    }
}

would you like to lookup recursively? Then you can use a generator...

let data = [{ "page": "main", "content": [{ "type": "item", "item": [{ "name": "HAMMER", "price": "1000" }, { "name": "SWORD", "price": "2000" }] }] }]

function* iterate(arrayLike, prop, value) {
    for (let page of arrayLike) {
        if (page[prop] == value)
            yield page;

        for (let v of Object.values(page)) {
            if (Array.isArray(v))
                yield* iterate(v, prop, value)
        }
    }
}

for (let item of iterate(data, "name", "HAMMER")) {
    console.log(item)
}

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!