Home > Enterprise >  how to find a diff of object keys nested in array
how to find a diff of object keys nested in array

Time:11-05

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
}

CodePudding user response:

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

CodePudding user response:

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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

you can achieve this using a for loop. I just hardcoded inside the loop for simplicity

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
}


for (let i = 0; i < main.cost.length; i  ) {
  main.cost[i].value = main.cost[i].value-other.cost[i].value-other2.cost[i].value;
}

console.log(main)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related