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

109
Vistas
nested for loops to get another object-array output

I need to multiply all the "values" inside "obj1" with the "percent' inside obj2 based on the id of each object. What would be the best way to do that? I've tried with for loop and reduce but I wasn't successful. Any help will be appreciated.

const obj1 = [ { id: 1, value: 10 }, { id: 2, value: 10 } ]

const obj2 = {
  len: {
      id: 1,
      nj: "321345", 
      percent: 0.05,
  },
  wor: {
      id: 2,
      nj: "321345", 
      percent: 0.1,
  }
}

outputExpected: [ { id: 1, value: 0.5 }, { id: 2, value: 1 } ]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can do that by going through and matching the ids. there are some optimizations that can be made if they are sorted however.

const obj1 = [ { id: 1, value: 10 }, { id: 2, value: 10 } ]

const obj2 = {
  len: {
      id: 1,
      nj: "321345", 
      percent: 0.05,
  },
  wor: {
      id: 2,
      nj: "321345", 
      percent: 0.1,
  }
}
const x = Object.keys(obj2).map((key,index)=>{
  const { id, value } = obj1.find(({id})=>id===obj2[key].id)
  return ({id,value:value*obj2[key].percent})
})
console.log(x)
//outputExpected: [ { id: 1, value: 0.5 }, { id: 2, value: 1 } ]

about 4 years ago · Juan Pablo Isaza Denunciar

0

This should work for you but doesnt handle exeptions if the id doesnt exist in both objects.

// First get the values in an array for easier manipulation
const aux = Object.values(obj2)

const output = obj1.map(ob => {
  // Find the id in the other array.
  const obj2Ob = aux.find(o => o.id === ob.id) // The id must exist in this aproach
  
  return {
    id: ob.id,
    value: ob.value * obj2Ob.percent
  }
})

console.log(output) // [ { id: 1, value: 0.5 }, { id: 2, value: 1 } ]
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can first create a lookup map using Map, then loop over the obj1 using map to get the desired result

const obj1 = [
  { id: 1, value: 10 },
  { id: 2, value: 10 },
];

const obj2 = {
  len: {
    id: 1,
    nj: "321345",
    percent: 0.05,
  },
  wor: {
    id: 2,
    nj: "321345",
    percent: 0.1,
  },
};

const map = new Map();
Object.values(obj2).forEach((v) => map.set(v.id, v));

const result = obj1.map((o) => ({ ...o, value: o.value * map.get(o.id).percent }));

console.log(result);

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