javascript - 合并两个嵌套级别的对象数组并返回一个键对象数组

标签 javascript arrays

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

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

如何获得如下所示的预期数据。需要从响应中选择 sectionType 并找到标签。

我需要通过合并响应和数据来获得 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)

最佳答案

你可以拿一个Map对于标签并通过使用值和标签创建新对象来映射响应

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

关于javascript - 合并两个嵌套级别的对象数组并返回一个键对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288867/

相关文章:

javascript - jsonp内存泄漏

javascript - 为什么要在 AJAX 删除请求的 url 中用空格替换 - ?

c++ - 在不同的文件中定义指针数组长度(C++)

php - 按数组中的指定列对行进行分组

arrays - PostgreSQL 中是否有可用的多值字段类型?

javascript - 在 javascript/node.js 中迭代对象数组的有效方法

javascript - Promise 内 bcrypt 的 Promise {<pending>}

javascript - Firebase 网络应用程序 - 创建新帐户时无法获取 user.uid

Javascript 输入数字

c - C中两个数组相加