I have multiple array elements. I want to combine all those array elements into single element.
My array elements:
[
{"2627":{"pid":"2619","level":"2","mid":"2627","name":"EXCEPTION HOURS","children":[]}},
{"2626":{"pid":"2619","level":"2","mid":"2626","name":"AVAIL TIME","children":[]}},
{"3058":{"pid":"2619","level":"2","mid":"3058","name":"WORK CENTER","children":[]}},
{"3057":{"pid":"2619","level":"2","mid":"3057","name":"CENTRAL OFFICE","children":[]}},
{"2605":{"pid":"2619","level":"2","mid":"2605","name":"IDLING","children":[]}},
{"2607":{"pid":"2619","level":"2","mid":"2607","name":"COMPLIANT RETURN %","children":[]}},
{"2608":{"pid":"2619","level":"2","mid":"2608","name":"COMPLIANT DEPART %","children":[]}}
]
After combining the array elements, the output should look like below:
{
"2627":{"pid":"2619","level":"2","mid":"2627","name":"EXCEPTION HOURS","children":[]},
"2626":{"pid":"2619","level":"2","mid":"2626","name":"AVAIL TIME","children":[]},
"3058":{"pid":"2619","level":"2","mid":"3058","name":"WORK CENTER","children":[]},
"3057":{"pid":"2619","level":"2","mid":"3057","name":"CENTRAL OFFICE","children":[]},
"2605":{"pid":"2619","level":"2","mid":"2605","name":"IDLING","children":[]},
"2607":{"pid":"2619","level":"2","mid":"2607","name":"COMPLIANT RETURN","children":[]},
"2608":{"pid":"2619","level":"2","mid":"2608","name":"COMPLIANT DEPART","children":[]},
}
I am using array reduce code like below, but it is not working
const output = input.reduce((a, obj) => {
a[obj.mid] = obj;
return a;
}, {});
console.log(output);
const input = [
{"2627":{"pid":"2619","level":"2","mid":"2627","name":"EXCEPTION HOURS","children":[]}},
{"2626":{"pid":"2619","level":"2","mid":"2626","name":"AVAIL TIME","children":[]}},
{"3058":{"pid":"2619","level":"2","mid":"3058","name":"WORK CENTER","children":[]}},
{"3057":{"pid":"2619","level":"2","mid":"3057","name":"CENTRAL OFFICE","children":[]}},
{"2605":{"pid":"2619","level":"2","mid":"2605","name":"IDLING","children":[]}},
{"2607":{"pid":"2619","level":"2","mid":"2607","name":"COMPLIANT RETURN %","children":[]}},
{"2608":{"pid":"2619","level":"2","mid":"2608","name":"COMPLIANT DEPART %","children":[]}}
]
const output = input.reduce((a, obj) => {
a[obj.mid] = obj;
return a;
}, {});
console.log(output);