【问题标题】:Create JSON object from Array and Multi-Dimensional Array从数组和多维数组创建 JSON 对象
【发布时间】:2022-01-19 06:04:17
【问题描述】:

我有这个 JSON 对象:

{
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
}

我想将 Javascript 中的那个对象转换成这个对象:

{
  reportResults: [{
      "Incident ID": "3599590",
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    },
    {
      "Incident ID": "3599591",
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    }
  ]
}

我已经尝试在以下示例中使用推送功能:

VWA_Output = {
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
};

JSTest_JSON_Var1 = {
  reportResults: []
};
for (i in VWA_Output.rows) {
  for (var j in VWA_Output.rows[i]) {
    var key = VWA_Output.columnNames[j];
    var value = VWA_Output.rows[i][j]
    JSTest_JSON_Var1.reportResults.push({
      [key]: value
    });

  }
}
console.log(JSTest_JSON_Var1);

但是,它似乎使用集合作为单独的数组元素来创建这样的对象:

{
  [{
    "reportResults": [{
        "Incident ID": "3599590"
      }, {
        "IncidentType": "Telecommuting/VWA Empl- Initiate"
      }
    },
    {
      "Incident ID": "3599591"
    },
    {
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    }
  }]
}

我希望列和行的集合成为数组中的单个记录集合:

{
  "reportResults": [{
    "Incident ID": "3599590",
    "IncidentType": "Telecommuting/VWA Empl- Initiate"
  }, {
    "Incident ID": "3599591",
    "IncidentType": "Telecommuting/VWA Empl- Initiate"
  }]
}

谢谢!

【问题讨论】:

  • 如果您在内循环之前创建一个空对象,您尝试的方法可能会起作用。然后在内循环内部将键和值分配给它作为obj[key] = value,最后在内循环外部将该对象推送到JSTest_JSON_Var1.reportResults。但我认为你应该选择下面提供的更具可读性的答案。
  • 所有提供的方法都通过我在浏览器中的测试工作。我正在使用 Oracle SOA Suite 中的 Javascript 组件来执行解析和对象重建,不幸的是它不喜欢 map 函数调用(该组件基于 Mozilla Rhino Javascript 引擎)。我最终按照airer301的建议更改了我的原始代码以推入外循环。

标签: javascript arrays json multidimensional-array


【解决方案1】:

一次性定义reportResults - 使其内容是从rows 映射的数组,您可以在其中使用要迭代的列名的索引来访问适当的行值。我会使用Object.fromEntries 来保持简洁。

const input = {
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
};
const output = {
  reportResults: input.rows.map(row => Object.fromEntries(
    input.columnNames.map((name, i) => [name, row[i]])
  ))
};
console.log(output);

【讨论】:

    【解决方案2】:

    假设您的示例数据位于data

    const result = {
      "reportResults" : data.rows.map(row => {
        return {
          [data.columnNames[0]]: row[0],
          [data.columnNames[1]]: row[1]
        }
      })
    }
    

    【讨论】:

      【解决方案3】:

      这是我的解决方案:

      var data = {
        "columnNames": [
          "Incident ID",
          "IncidentType"
        ],
        "rows": [
          [
            "3599590",
            "Telecommuting/VWA Empl- Initiate"
          ],
          [
            "3599601",
            "Telecommuting/VWA Empl- Initiate"
          ]
        ]
      }
      
      var reportResults = data.rows.map((row) => 
          Object.assign({}, ...row.map((cell, i) => ({ [data.columnNames[i]]: cell})))
      )
      
      console.log({reportResults})
      

      不是最佳的,但很短)

      【讨论】:

        【解决方案4】:

        请参阅上面的我的 cmets。提供的每个答案都在浏览器中有效,但在我使用的 SOA Suite Javascript 组件中无效。该组件不喜欢 map 函数调用。再次感谢所有回复。

        以下是 Oracle SOA Suite JS 组件的工作原理:

        process.VWA_Output = {
          "columnNames": [
            "Incident ID",
            "IncidentType"
          ],
          "rows": [
            [
              "3599590",
              "Telecommuting/VWA Empl- Initiate"
            ],
            [
              "3599601",
              "Telecommuting/VWA Empl- Initiate"
            ]
          ]
        };
        process.JSTest_JSON_Var1 = { 
          reportResults: [] 
        }; 
        
        for (i in process.VWA_Output.rows) { 
          const row = new Object(); 
          for (var j in process.VWA_Output.rows[i]) { 
            var key = process.VWA_Output.columnNames[j]; 
            var value = process.VWA_Output.rows[i][j]; 
            row[key] = value; 
          } 
          process.JSTest_JSON_Var1.reportResults.push(row); 
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-02
          • 2021-06-16
          • 2018-07-03
          • 1970-01-01
          • 2011-07-28
          • 2014-11-03
          • 2014-08-14
          • 1970-01-01
          相关资源
          最近更新 更多