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

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

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

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

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 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