1

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);
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
Sricharan
  • 395
  • 1
  • 16
  • @Nina Scholz, It is not a duplicate, I don't want to merge the objects, I just want to combine them into single object. – Sricharan Jun 22 '19 at 08:19

2 Answers2

2

You can simply use Object.assign() method e.g:

const data = [
    {"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 result = Object.assign({}, ...data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mohammad Usman
  • 30,882
  • 16
  • 80
  • 78
  • I have tried this, but is changing sorting order. In my case, the order should not be changed – Sricharan Jun 22 '19 at 08:11
  • @sony the js spec specifies the order of objects with numeric keys. If you want an object you can’t have the order you want. If you want an ordered collection in a different order you need an array. – Mark Jun 22 '19 at 08:14
  • @sony Once you have your data inside an object form, order of properties is not guaranteed. See [this post](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) for more details. – Mohammad Usman Jun 22 '19 at 08:15
  • @MohammadUsman that’s pretty old info. The order is guaranteed in modern JS in this case. – Mark Jun 22 '19 at 08:16
  • @MarkMeyer Not guaranteed on all platforms IIRC – Jack Bashford Jun 22 '19 at 08:40
2

Extract the first element from the Object.entries of each object being iterated over:

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) => {
  const [key, val] = Object.entries(obj)[0];
  a[key] = val;
  return a;
}, {});
console.log(output);
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
  • Thank you for the answer, but the order of the objects should not be changed. it should be in order like 2627, 2626, 3058 etc... – Sricharan Jun 22 '19 at 08:14
  • Numeric object properties cannot really be ordered - when iterating over an object (like when the object's keys and values are logged when using `console.log`), the lower number keys will *always* come before the higher number keys. There's no way around it. If you need an *ordered* collection, don't use an object - use an array. – CertainPerformance Jun 22 '19 at 08:15