首页 > 解决方案 > 合并两个嵌套级别的对象数组并返回一个关键对象数组

问题描述

我有一个名为 response 的对象数组,其中每个键都有一个值,现在我需要返回一个包含此键和值的数组,但需要从另一个包含键标签名称的对象中获取标签。

键的标签名称是嵌套级别的,位于数据变量内部。

我怎样才能得到预期的数据,如下所示。需要从响应中选择sectionType并找到标签

任何帮助表示赞赏。需要通过合并response和data得到expectedData变量结果

    const response = [
  {
    sectionType: 'section1',
    test1: 'test 1 data',
    test2: '',
    test3: 'test 3 data',
  },
  {
    sectionType: 'section3',
    test1: 'test 1 data',
    test2: 'test 2 data',
    test4: 'test 4 data',
  },
  {
    sectionType: 'section2',
    test1: 'test 1 data',
    test2: 'test 2 data',
    test5: 'test 5 data',
  },
  {
    sectionType: 'section1',
    test1: 'test 1 data',
    test6: '',
    test3: 'test 3 data',
  },
  {
    sectionType: 'section3',
    test1: '',
    test2: 'test 2 data',
    test3: 'test 3 data',
  },
];

const data = {
  section1: {
    forms: [
      {
        fields: [
          {
            columnName: 'test1',
            label: [{ actualLabel: 'Test 1' }],
          },
          {
            columnName: 'test2',
            label: [{ actualLabel: 'Test 2' }],
          },
          {
            columnName: 'test0',
            label: [{ actualLabel: 'Test 0' }],
          },
        ],
      },
      {
        fields: [
          {
            columnName: 'test6',
            label: [{ actualLabel: 'Test 6' }],
          },
          {
            columnName: 'test3',
            label: [{ actualLabel: 'Test 3' }],
          },
          {
            columnName: 'test10',
            label: [{ actualLabel: 'Test 10' }],
          },
        ],
      },
      {
        fields: [
          {
            columnName: 'test15',
            label: [{ actualLabel: 'Test 15' }],
          },
          {
            columnName: 'test',
            label: [{ actualLabel: 'Test 6' }],
          },
          {
            columnName: 'test7',
            label: [{ actualLabel: 'Test 7' }],
          },
        ],
      },
    ],
  },
  section2: {
    forms: [
      {
        fields: [
          {
            columnName: 'test1',
            label: [{ actualLabel: 'Test 1' }],
          },
          {
            columnName: 'test2',
            label: [{ actualLabel: 'Test 2' }],
          },
          {
            columnName: 'test0',
            label: [{ actualLabel: 'Test 0' }],
          },
        ],
      },
      {
        fields: [
          {
            columnName: 'test3',
            label: [{ actualLabel: 'Test 3' }],
          },
          {
            columnName: 'test4',
            label: [{ actualLabel: 'Test 4' }],
          },
          {
            columnName: 'test10',
            label: [{ actualLabel: 'Test 10' }],
          },
        ],
      },
      {
        fields: [
          {
            columnName: 'test5',
            label: [{ actualLabel: 'Test 5' }],
          },
          {
            columnName: 'test6',
            label: [{ actualLabel: 'Test 6' }],
          },
          {
            columnName: 'test7',
            label: [{ actualLabel: 'Test 7' }],
          },
        ],
      },
    ],
  },
  section3: {
    forms: [
      {
        fields: [
          {
            columnName: 'test1',
            label: [{ actualLabel: 'Test 1' }],
          },
          {
            columnName: 'test2',
            label: [{ actualLabel: 'Test 2' }],
          },
          {
            columnName: 'test0',
            label: [{ actualLabel: 'Test 0' }],
          },
        ],
      },
      {
        fields: [
          {
            columnName: 'test3',
            label: [{ actualLabel: 'Test 3' }],
          },
          {
            columnName: 'test 4',
            label: [{ actualLabel: 'Test 4' }],
          },
          {
            columnName: 'test10',
            label: [{ actualLabel: 'Test 10' }],
          },
        ],
      },
      {
        fields: [
          {
            columnName: 'test15',
            label: [{ actualLabel: 'Test 15' }],
          },
          {
            columnName: 'test6',
            label: [{ actualLabel: 'Test 6' }],
          },
          {
            columnName: 'test7',
            label: [{ actualLabel: 'Test 7' }],
          },
        ],
      },
    ],
  },
};

let expectedData = [
  {
    test1: { value: 'test 1 data', label: 'Test 1' },
    test2: { value: '', label: 'Test 2' },
    test3: { value: 'test 3 data', label: 'Test 3' },
  },
  {
    test1: { value: 'test 1 data', label: 'Test 1' },
    test2: { value: 'test 2 data', label: 'Test 2' },
    test4: { value: 'test 4 data', label: 'Test 4' },
  },
  {
    test1: { value: 'test 1 data', label: 'Test 1' },
    test2: { value: 'test 2 data', label: 'Test 2' },
    test5: { value: 'test 5 data', label: 'Test 5' },
  },
  {
    test1: { value: 'test 1 data', label: 'Test 1' },
    test6: { value: '', label: 'Test 6' },
    test3: { value: 'test 3 data', label: 'Test 3' },
  },
  {
    test1: { value: '', label: 'Test 1' },
    test2: { value: 'test 2 data', label: 'Test 2' },
    test3: { value: 'test 3 data', label: 'Test 3' },
  },
];

尝试使用以下代码,但获取对象但不完全。有没有更好的方法来实现这一点。

let keysCollection = []

response.forEach(d => {
  let keys = Object.keys(d);
  keysCollection.push(keys)
})


let mergingKeysCollection = keysCollection.reduce((a,b) => [...a, ...b], [])

let uniqueKeys = Array.from(new Set(mergingKeysCollection))

let actualData = Object.keys(data)

标签: javascriptarrays

解决方案


您可以将 aMap作为标签并response通过使用值和标签创建新对象来映射。

var response = [{ sectionType: 'section1', test1: 'test 1 data', test2: '', test3: 'test 3 data' }, { sectionType: 'section3', test1: 'test 1 data', test2: 'test 2 data', test4: 'test 4 data' }, { sectionType: 'section2', test1: 'test 1 data', test2: 'test 2 data', test5: 'test 5 data' }, { sectionType: 'section1', test1: 'test 1 data', test6: '', test3: 'test 3 data' }, { sectionType: 'section3', test1: '', test2: 'test 2 data', test3: 'test 3 data' }],
    data = { section1: { forms: [{ fields: [{ columnName: 'test1', label: [{ actualLabel: 'Test 1' }] }, { columnName: 'test2', label: [{ actualLabel: 'Test 2' }] }, { columnName: 'test0', label: [{ actualLabel: 'Test 0' }] }] }, { fields: [{ columnName: 'test6', label: [{ actualLabel: 'Test 6' }] }, { columnName: 'test3', label: [{ actualLabel: 'Test 3' }] }, { columnName: 'test10', label: [{ actualLabel: 'Test 10' }] }] }, { fields: [{ columnName: 'test15', label: [{ actualLabel: 'Test 15' }] }, { columnName: 'test', label: [{ actualLabel: 'Test 6' }] }, { columnName: 'test7', label: [{ actualLabel: 'Test 7' }] }] }] }, section2: { forms: [{ fields: [{ columnName: 'test1', label: [{ actualLabel: 'Test 1' }] }, { columnName: 'test2', label: [{ actualLabel: 'Test 2' }] }, { columnName: 'test0', label: [{ actualLabel: 'Test 0' }] }] }, { fields: [{ columnName: 'test3', label: [{ actualLabel: 'Test 3' }] }, { columnName: 'test4', label: [{ actualLabel: 'Test 4' }] }, { columnName: 'test10', label: [{ actualLabel: 'Test 10' }] }] }, { fields: [{ columnName: 'test5', label: [{ actualLabel: 'Test 5' }] }, { columnName: 'test6', label: [{ actualLabel: 'Test 6' }] }, { columnName: 'test7', label: [{ actualLabel: 'Test 7' }] }] }] }, section3: { forms: [{ fields: [{ columnName: 'test1', label: [{ actualLabel: 'Test 1' }] }, { columnName: 'test2', label: [{ actualLabel: 'Test 2' }] }, { columnName: 'test0', label: [{ actualLabel: 'Test 0' }] }] }, { fields: [{ columnName: 'test3', label: [{ actualLabel: 'Test 3' }] }, { columnName: 'test 4', label: [{ actualLabel: 'Test 4' }] }, { columnName: 'test10', label: [{ actualLabel: 'Test 10' }] }] }, { fields: [{ columnName: 'test15', label: [{ actualLabel: 'Test 15' }] }, { columnName: 'test6', label: [{ actualLabel: 'Test 6' }] }, { columnName: 'test7', label: [{ actualLabel: 'Test 7' }] }] }] } },
    labels = Object.entries(data).reduce((l, [k, { forms }]) => {
        forms.forEach(({ fields }) =>
            fields.forEach(({ columnName, label: { 0: { actualLabel } } }) =>
                l.set([k, columnName].join('|'), actualLabel)
            )
        );
        return l;
    }, new Map),
    result = response.map(({ sectionType, ...rest }) =>
        Object.assign({}, ...Object.entries(rest).map(([k, value]) =>
            ({ [k]: { value, label: labels.get([sectionType, k].join('|')) } }))
        )
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }


推荐阅读