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

170
Vistas
How to convert an object with arrays into an array

I have a problem in reducing an object which contains array into a simple array. I have a backend which returns a list of dogs and their price. The way the api returns the data, seems really difficult to work with and I am struggling to convert the object into an array. I have tried turning into an array and reducing it.

Here is an example - I want to convert the following object:

const a = {
    dogs: [{
            "id": "dog1",
            "priceRange": [
                "low",
                "high"
            ],
            "vaccinated": true,
        },
        {
            "id": "dog2",
            "priceRange": [
                "low",
                "high"
            ],
            "vaccinated": false,
        }
    ],
    "cost": [{
            "id": "low",
            "cost": 200,
        },
        {
            "id": "mid",
            "cost": 400,
        },
        {
            "id": "high",
            "cost": 600,
        }
    ]
};

into this array:

const reducedArray = [{
        "id": "dog1",
        "priceRange": [{
                "id": "low",
                "cost": 200,
            },
            {
                "id": "high",
                "cost": 600,
            }
        ],
        "vaccinated": true,
    },
    {
        "id": "dog2",
        "priceRange": [{
                "id": "low",
                "cost": 200,
            },
            {
                "id": "high",
                "cost": 600,
            }
        ],
        "vaccinated": false,
    }
]

I am not sure

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to iterate over your a.dogs array first and within each iteration, also iterate over the priceRange array and find its corresponding value inside the a.cost array and return that instead of the string low or high.

a.dogs.map(dog => ({
    ...dog,
  priceRange: dog.priceRange.map(priceRange => a.cost.find(cost => cost.id === priceRange))
}))
about 4 years ago · Juan Pablo Isaza Denunciar

0

  • Using Array#reduce, iterate over a.cost while updating a Map where the key is the id and value is the object
  • Using Array#map, iterate over a.dogs and set the priceRannge items from the Map using another Array#map iteraion

const a = {
  "dogs": [ { "id": "dog1", "priceRange": [  "low", "high" ], "vaccinated": true }, { "id": "dog2", "priceRange": [ "low", "high" ], "vaccinated": false } ],
  "cost": [ { "id": "low", "cost": 200 }, { "id": "mid", "cost": 400 }, { "id": "high", "cost": 600 } ]
};

const costMap = a.cost.reduce((map, cost) => 
  map.set(cost.id, cost)
, new Map);

const reducedArray = a.dogs.map(dog => ({
  ...dog, 
  priceRange: dog.priceRange.map(range => {
    const { id, cost } = costMap.get(range) || {};
    return { id, cost};
  })
}));

console.log(reducedArray);

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