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

165
Views
How can I get items in data array with recursive?

I have a data array

var data=[{
"key": "KUZEY",
"items": [
    {
        "key": "MARMARA",
        "items": [
            {
                "key": "T100",
                "items": [
                    {
                        "Ref": 1,
                        "ApprovedReserveQuantity": 1
                       
                    }
                ]
            }
        ]
    },
    {
        "key": "MARMARA 2",
        "items": [
            {
                "key": "T100",
                "items": [
                    {
                        "Ref": 2,
                        "ApprovedReserveQuantity": 1
                        
                   }
                ]
            }
        ]
    }
] }]

İ want to get items when i call function. how can do that recursiveMethod?

groupedItems=recursiveMethod(data)

groupedItems==>[{"Ref": 1,"ApprovedReserveQuantity": 1},{"Ref": 2,"ApprovedReserveQuantity": 1}]

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

0

groupedItems:any[]=[];    
recursiveMethod(element){
          if(element.items==null)
            this.groupedItems.push(element)
          if (element.items != null){
              let i;
              for(i=0;  i < element.items.length; i++){
                    this.recursiveMethod(element.items[i]);
              }
         }
    }

it's worked

about 4 years ago · Juan Pablo Isaza Report

0

Couldn't find any 'key' checking in your answer. Even though I don't trust my function completely, and am confused for as why it worked, It can be reusable if you tweak/adjust it.

const extractInnermostByKey = (data, targetKey, res = []) => {
    data.forEach((obj) => {
        for (let key of Object.keys(obj)) {
            if (key === targetKey) {
                // console.log(res);  observe res
                res.shift();
                res.push(...obj[key]);
                return extractInnermostByKey(res, targetKey, res);
            }
        }
    });
    return res;
};

const groupedItems = extractInnermostByKey(data, 'items');

console.log(groupedItems);
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!