2

I have an array of objects like this-

    {"zone":"lobby a","time":"00:00","hub":"I","Sum":0.0,"schedule":"2022"},
    {"zone":"lobby b","time":"00:01","hub":"I","Sum":1.0,"schedule":"112022"},
    {"zone":"lobby a","time":"00:01","hub":"I","Sum":2.0,"schedule":"11_2022"},
    {"zone":"lobby b","time":"00:03","hub":"I","Sum":0.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":1.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":2.0,"schedule":"11_2022"},

The duplicate 'time' needs to be merged by adding the sums only if the zone is same.

Result should be

    {"zone":"lobby a","time":"00:00","hub":"I","Sum":0.0,"schedule":"2022"},
    {"zone":"lobby b","time":"00:01","hub":"I","Sum":1.0,"schedule":"112022"},
    {"zone":"lobby a","time":"00:01","hub":"I","Sum":2.0,"schedule":"11_2022"},
    {"zone":"lobby b","time":"00:03","hub":"I","Sum":0.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":3.0,"schedule":"11_2022"},
      

Any help will be much appreciated. I have tried many options using maps etc, but have not been successful.

Nehal
  • 13,130
  • 4
  • 43
  • 59
Aditya R
  • 383
  • 2
  • 7
  • 21
  • 3
    Does this answer your question? [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – ericmp Oct 10 '22 at 11:53

3 Answers3

1
let input = [
{"zone":"lobby a","time":"00:00","hub":"I","Sum":0.0,"schedule":"2022"},
    {"zone":"lobby b","time":"00:01","hub":"I","Sum":1.0,"schedule":"112022"},
    {"zone":"lobby a","time":"00:01","hub":"I","Sum":2.0,"schedule":"11_2022"},
    {"zone":"lobby b","time":"00:03","hub":"I","Sum":0.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":1.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":2.0,"schedule":"11_2022"},
    ]

    let cleared = [];

    input.map((item, index)=>{
    const dupl_index = cleared.findIndex(itm_ =>itm_.zone === item.zone && itm_.time === item.time);
    if(dupl_index === -1){
    cleared.push(item)
    }else{
    cleared[dupl_index].Sum = Number(cleared[dupl_index].Sum + item.Sum).toFixed(1)
    }
    })

    console.log(cleared)
Ingenious_Hans
  • 724
  • 5
  • 16
1

The following function uses a grouping function to associate items with one another, and a merging function to perform the addition of the sums.

For each item in the data, the group is determined and the group is replaced by the result of the existing group merged with the current item in that group.

Time complexity is O(n) (one iteration to group the items, one iteration to enumerate the resulting grouped items).

const data = [
    {"zone":"lobby a","time":"00:00","hub":"I","Sum":0.0,"schedule":"2022"},
    {"zone":"lobby b","time":"00:01","hub":"I","Sum":1.0,"schedule":"112022"},
    {"zone":"lobby a","time":"00:01","hub":"I","Sum":2.0,"schedule":"11_2022"},
    {"zone":"lobby b","time":"00:03","hub":"I","Sum":0.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":1.0,"schedule":"11_2022"},
    {"zone":"lobby c","time":"00:04","hub":"I","Sum":2.0,"schedule":"11_2022"} ]

function groupAndMerge(data, groupFn, mergeFn) {
    const result = {}
    for(let d of data) {
        result[groupFn(d)] = mergeFn(d, result[groupFn(d)] || { Sum: 0 }) 
    }
    return Object.values(result)
}

const groupFn = ({zone, time}) => zone + time
const mergeFn = (a, b) => ({ ...a, Sum: a.Sum + b.Sum })
console.log(groupAndMerge(data, groupFn, mergeFn))
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • Thank you very much. This worked, The other solutions somehow caused issues on the React states( which I'm setting using this array) – Aditya R Oct 12 '22 at 05:59
1

const a = [
  {"zone":"lobby a","time":"00:00","hub":"I","Sum":0.0,"schedule":"2022"},
  {"zone":"lobby b","time":"00:01","hub":"I","Sum":1.0,"schedule":"112022"},
  {"zone":"lobby a","time":"00:01","hub":"I","Sum":2.0,"schedule":"11_2022"},
  {"zone":"lobby b","time":"00:03","hub":"I","Sum":0.0,"schedule":"11_2022"},
  {"zone":"lobby c","time":"00:04","hub":"I","Sum":1.0,"schedule":"11_2022"},
  {"zone":"lobby c","time":"00:04","hub":"I","Sum":2.0,"schedule":"11_2022"}
];

let r = [];
a.forEach(i => {
  let dup = r.find(j => j.zone===i.zone && j.time===i.time);
  if(dup) dup.Sum = (dup.Sum + i.Sum).toFixed(1);
  else r.push(i);
});
console.log(r);
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27