过滤键并简化为嵌套结构

时间:2019-05-31 09:26:15

标签: javascript arrays object ecmascript-6

我有一个下面的数据结构,我需要提取列,这是一个数组,我需要用键和实际标签名来构建对象。

有没有更好的方法来实现这一目标。

任何帮助表示赞赏

    let 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 extractColumns = ['test1', 'test2', 'test7', 'test15']
    
   let result = Object.entries(data).reduce(
          (initial, [key, { forms }]) => {
            forms.forEach(({ fields }) => {
              fields.forEach(
                ({
                  columnName,
                  label: {
                    0: { actualLabel },
                  },
                }) => {
                  if (extractColumns.indexOf(columnName) > -1) {
                    initial[columnName] = {
                      actualLabel,
                    };
                  }
                },
              );
            });
            return initial;
          },
          {},
        );

console.log(result)

3 个答案:

答案 0 :(得分:1)

您可以像这样简化和压缩代码-使用Object.valuesincludes,三元运算符和简化的数组分解。

let 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 extractColumns = ['test1', 'test2', 'test7', 'test15'];

const result = Object.values(data).reduce((acc, { forms }) => {
  forms.forEach(({ fields }) => fields.forEach(({ columnName, label: [{ actualLabel }]}) => extractColumns.includes(columnName) ? acc[columnName] = { actualLabel } : null));
  return acc;
}, {});

console.log(result)

答案 1 :(得分:1)

另一个选择是使用flatMap获取字段数组,然后使用filter获取输出。您可以创建Set而不是列数组,因为Set#hasincludesmuch faster

let 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 extractColumns = new Set(['test1', 'test2', 'test7', 'test15'])

const output = Object.values(data).reduce((acc, { forms }) => {
  forms.flatMap(a => a.fields)
        .filter(f => extractColumns.has(f.columnName))
        .forEach(({ columnName, label: [o] }) => acc[columnName] = o)

  return acc;
}, {})

console.log(output)

(我没有检查它是否比您已经拥有的要快)

答案 2 :(得分:0)

我提出了更一般的解决方案。受启发的通天塔。

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"
                        }]
                    }
                ]
            }
        ]
    }
}


function walking(o, callback, result) {
    if (Array.isArray(o)) {
        callback('array', o, result);
        for (let i = 0; i < o.length; i++) {
            walking(o[i], callback, result);
        }
    } else if (typeof o === 'object') {
        callback('object', o, result);
        for (let name in o) {
            walking(o[name], callback, result);
        }
    } else {
        callback('primitive', o, result);
    }
    return result;
}


function handler(type, value, result) {
    const extractColumns = ['test1', 'test2', 'test7', 'test15'];
    if (type == 'object') {
        if ('columnName' in value && 'label' in value) {
            if (extractColumns.indexOf(value.columnName) !== -1) {
                result[value.columnName] = value.label[0];
            }
        }
    }
}

console.log(walking(data, handler, Object.create(null)));