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

158
Views
how to find a diff of object keys nested in array

I have 3 objects, they have a "cost" key which is array of objects. As a result, I want to have a "main" which will stay the same except its "value" will be a diff of this value minus other objects "value"

const main = {
  cost: [
    { id: 'main', value: 20, timestapm: 'asd', current: '10'},
    { id: 'main', value: 10, timestapm: 'asd', current: '10'},
    { id: 'main', value: 18, timestapm: 'asd', current: '10'},
  ],
  description: 'maindevice',
  total: 5
}

const other = {
  cost: [
    { id: 'device1', value: 10, timestapm: 'qwe', current: '10'},
    { id: 'device1', value: 5, timestapm: 'qwe', current: '10'},
    { id: 'device1', value: 9, timestapm: 'qwe', current: '10'},
  ],
  description: 'maindevice',
  total: 3
}

const other2 = {
  cost: [
    { id: 'device2', value: 5, timestapm: 'zxc', current: '10'},
    { id: 'device2', value: 2, timestapm: 'zxc', current: '10'},
    { id: 'device2', value: 2, timestapm: 'zxc', current: '10'},
  ],
  description: 'maindevice',
  total: 6
}

const devices = [main, other, other2];

result i want to have => 

main = {
  cost: [
    { id: 'main', value: 5, timestapm: 'asd', current: '10'},
    { id: 'main', value: 3, timestapm: 'asd', current: '10'},
    { id: 'main', value: 7, timestapm: 'asd', current: '10'},
  ],
  description: 'maindevice',
  total: 5
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const calcNewValue = (main, other, other2) => {
    main.cost = main.cost.map((obj, index) => { return {...obj, value: value=other.cost[index].value - other2.cost[index].value}})
    return main
}

This will work for you

about 4 years ago · Juan Pablo Isaza Report

0

You could reduce devices and mutate the first object of the array.

const
    main = { cost: [{ id: 'main', value: 20, timestamp: 'asd', current: '10'}, { id: 'main', value: 10, timestapm: 'asd', current: '10'}, { id: 'main', value: 18, timestapm: 'asd', current: '10'}], description: 'maindevice', total: 5 },
    other = { cost: [{ id: 'device1', value: 10, timestamp: 'qwe', current: '10'}, { id: 'device1', value: 5, timestapm: 'qwe', current: '10'}, { id: 'device1', value: 9, timestapm: 'qwe', current: '10'}], description: 'maindevice', total: 3 },
    other2 = { cost: [{ id: 'device2', value: 5, timestamp: 'zxc', current: '10'}, { id: 'device2', value: 2, timestapm: 'zxc', current: '10'}, { id: 'device2', value: 2, timestapm: 'zxc', current: '10'}], description: 'maindevice', total: 6 },
    devices = [main, other, other2];

devices.reduce((r, o) => {
    o.cost.forEach(({ value }, i) => r.cost[i].value -= value);
    return r;
});

console.log(main);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!