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

140
Vistas
How to filter and transform object into two level object

I'm struggling with this problem. I have this object below.

 const originalData = {
  "price1": 100,
  "amount1": 17.46, 
  "price2": 500,
  "amount2": 29,
  "price3": 700,
  "amount3": 40.74
}

I want a new object inside an array like this below.

const newData =[
  {price:100, amount:17.46},
  {price:500, amount:29},
  {price:700, amount:40.74}      
],

I've tried these methods - filter/map/reduce and googled problom for ages to reach to the result that I wanted. but couldn't get it. please tell me how I can solve this problem.

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

0

Assuming the numbers for the keys are consecutive, you can divide the amount of keys by 2 to get the number of objects and create the array using Array#map or Array.from with the keys of the same number.

const originalData = {
  "price1": 100,
  "amount1": 17.46,
  "price2": 500,
  "amount2": 29,
  "price3": 700,
  "amount3": 40.74
}
let res = [...Array(Object.keys(originalData).length/2)]
            .map((_,i)=>({price: originalData[`price${i+1}`], 
                amount: originalData[`amount${i+1}`]}));
console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simply achieve it by using this logic :

const originalData = {
  "price1": 100,
  "amount1": 17.46, 
  "price2": 500,
  "amount2": 29,
  "price3": 700,
  "amount3": 40.74
};

const arr = [];

Object.keys(originalData).forEach((key, index) => {
  if (originalData[`price${index+1}`]) {
    const obj = {};
    obj.price = originalData[`price${index+1}`]
    obj.amount = originalData[`amount${index+1}`]
    arr.push(obj);
  }
});

console.log(arr);

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