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

183
Views
how to foreach array to array object in javascript

I'm very confused about doing foreach array to array object in Javascript, I already did a lot of research about foreach object in Javascript and I tried many ways but nothing works. All that I'm trying to achieve is to have data JSON like this :

[
            {
                "name": "First Data",
                "data": [
                    {
                        "y": 95,
                        "total":100,
                        "md": "1",
                        "name": "National",
                        "drillup" : 'level0',
                        "drilldown" : "3",
                        "next"  : "level2"
                    }

                ]
            }
            ,{
                "name": "Second Data",
                "data": [
                    {
                        "y": 95,
                        "total":100,
                        "md": "1",
                        "name": "National",
                        "drillup" : 'National',
                        "drilldown" : "3",
                        "next"  : "level2"
                    }

                ]
            }
        ]

and I tried to do foreach based on some finding of my research but the result wasn't like what I want or like what I'm try to achieve .. and here is the script that I tried :

  dataFirstSecond = await informationModel.getdata();
    Object.entries(dataRegularSecondary).forEach(entry => {
        const [key, value] = entry;
        returnData[key] = [
            {
                name: value.name,
                data: [{
                    y: value.y,
                    total: value.total_ada,
                    next: 'level_2',
                    drilldown: true,
                }]
            }]
    });

and here is the result or the output of my script that I try it :

{
    "0": [
        {
            "name": "First Data",
            "data": [
                {
                    "y": 22.973,
                    "total": 17,
                    "next": "level_2",
                    "drilldown": true
                }
            ]
        }
    ],
    "1": [
        {
            "name": "Second Data",
            "data": [
                {
                    "y": 5.4054,
                    "total": 4,
                    "next": "level_2",
                    "drilldown": true
                }
            ]
        }
    ]
}

can someone help me to achieve the data that I want?

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

0

returnData[key] = [{ ... }] should just be returnData.push({ ... }), and make sure returnData is an array (e.g. returnData = [])

about 4 years ago · Juan Pablo Isaza Report

0

If the function informationModel.getdata(); returns an Object you could use the method JSON.stringify(Object) to easily convert and Object to JSON. For example you could try to do to convert this Object to a String then cast the String to JSON.

let JSONString = JSON.stringify(informationModel.getdata());
let JSON_Object = JSON.parse(JSONString);
about 4 years ago · Juan Pablo Isaza Report

0

If dataRegularSecondary is an array and not an object you could use map:

dataRegularSecondary.map(value => {
      return {
        name: value.name,
        data: [{
          y: value.y,
          total: value.total_ada,
          next: 'level_2',
          drilldown: true,
        }]
      }
    }
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!