在JavaScript中将多个数组元素组合成单个对象

时间:2019-06-22 08:08:32

标签: javascript arrays json

我有多个数组元素。我想将所有这些数组元素组合成单个元素。

我的数组元素:

[
    {"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":[]}}
]

合并数组元素后,输出应如下所示:

{
    "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);

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

2 个答案:

答案 0 :(得分:2)

您可以简单地使用Object.assign()方法,例如:

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

答案 1 :(得分:2)

从要迭代的每个对象的Object.entries中提取第一个元素:

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