Search code examples
javascriptarraysobjectecmascript-6

Filtering the keys and reducing into nested structure


I have a below data structure. From it I need to extract the columns which is an array and I need to build an object with key and actual label name.

Is there any better way to achieve this?

    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)


Solution

  • You can simplify and compact the code like so - using Object.values, includes, ternary operators, and simplified array destructuring.

    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)