1

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
}

4
  • what does not work? Commented Nov 5, 2021 at 8:54
  • im stuck with how to do it. i can find a diff of just 3 arrays, but can't figure out how to do it when this arrays are nested in an object Commented Nov 5, 2021 at 8:56
  • do you want to mutate main? Commented Nov 5, 2021 at 9:02
  • @NinaScholz yeah i can mutate it Commented Nov 5, 2021 at 9:04

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.