Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda