Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!