Iterate through Json Object with nested Array Javascript

493
December 07, 2017, at 08:37 AM

I need to iterate this json and push the values into an other array

[
{
    "id": 1,
    "nombre": "Samson",
    "marcaModelo": [
        {
            "id": 6,
            "nombre": "API 6D "
        }
    ]
}
{
    "id": 6,
    "nombre": "Endress + Hauser",
    "marcaModelo": [
        {
            "id": 10,
            "nombre": "Proline t-mass 65F50-AK2AH1RCBBCA"
        },
        {
            "id": 8,
            "nombre": "Cerabar M"
        }
    ]
}
]

I made out with this

Object.entries(res).forEach((value, key) => {
      console.log(key, value)
      this.marcas.push({
        'marcaId': res[key].id,
        'marcaNombre': res[key].nombre,
        'modeloId': res[key].marcaModelo[0].id,
        'modeloNombre': res[key].marcaModelo[0].nombre
      })
      console.log(this.marcas)
    })

but only push one marcaModelo object into marcas array, if I replace marcaModelo[0] for marcaModelo[key] I get an undefined.

I need something like this

{
marcaId:6
marcaNombre:"Endress + Hauser"
modeloId:[10, 8]
modeloNombre:[Proline t-mass 65F50-AK2AH1RCBBCA, Cerabar M]
}
Answer 1

You need a nested loop for all the marcaModelo objects; you can use .map() to return an array of the results of a function on each element.. It also doesn't seem like you need to use Object.entries(). The original object is an array, you can use .forEach() directly on it.

res.forEach(value => {
    console.log(value);
    this.marcas.push({
        'marcaId': value.id,
        'marcaNombre': value.nombre,
        'modeloId': value.marcaModelo.map(o => o.id),
        'modeloNombre': value.marcaModelo.map(o => o.nombre)
    });
    console.log(this.marcas);
}
Answer 2

or

res.map(({ id, nombre, marcaModelo }) => ({
    marcaId: id,
    marcaNombre: nombre,
    modeloId: marcaModelo.map(m => m.id),
    modeloNombre: marcaModelo.map(m => m.nombre),
}));
Rent Charter Buses Company
READ ALSO
can I use an object's property in the object's method without using `this`

can I use an object's property in the object's method without using `this`

In following code, why I cannot access b without this keywordI am using nodejs to run the code

200
How to display JSON.stringify to a table?

How to display JSON.stringify to a table?

I have been working on a sort method for a class project and have successfully gotten it to sort my arraysHowever, I have took the sorted info and used the JSON

315
Is there a neat way to generate data to plot a Lorenz curve given a Gini coefficient?

Is there a neat way to generate data to plot a Lorenz curve given a Gini coefficient?

I want to compare flat rate vs progressive taxation approaches on populations with variously distributed incomesI'm happy to assume a smooth Lorenz distribution

177
Using nested IF/AND statements in Sheets in a single cell

Using nested IF/AND statements in Sheets in a single cell

I'm trying to create a formula that looks at a cell and determines what range the cell is between and then fills another cell with a specific value depending on the target cell's valueThis is the formula I've created:

222