Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda