首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从数组和多维数组创建JSON对象

从数组和多维数组创建JSON对象
EN

Stack Overflow用户
提问于 2021-12-16 05:10:42
回答 4查看 411关注 0票数 0

我有一个JSON对象:

代码语言:javascript
复制
{
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
}

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

代码语言:javascript
复制
{
  reportResults: [{
      "Incident ID": "3599590",
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    },
    {
      "Incident ID": "3599591",
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    }
  ]
}

在以下示例中,我尝试使用push函数:

代码语言:javascript
复制
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);

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

代码语言:javascript
复制
{
  [{
    "reportResults": [{
        "Incident ID": "3599590"
      }, {
        "IncidentType": "Telecommuting/VWA Empl- Initiate"
      }
    },
    {
      "Incident ID": "3599591"
    },
    {
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    }
  }]
}

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

代码语言:javascript
复制
{
  "reportResults": [{
    "Incident ID": "3599590",
    "IncidentType": "Telecommuting/VWA Empl- Initiate"
  }, {
    "Incident ID": "3599591",
    "IncidentType": "Telecommuting/VWA Empl- Initiate"
  }]
}

谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-12-16 05:18:03

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

代码语言:javascript
复制
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);

票数 1
EN

Stack Overflow用户

发布于 2021-12-16 05:19:12

假设示例数据在data

代码语言:javascript
复制
const result = {
  "reportResults" : data.rows.map(row => {
    return {
      [data.columnNames[0]]: row[0],
      [data.columnNames[1]]: row[1]
    }
  })
}
票数 2
EN

Stack Overflow用户

发布于 2021-12-16 05:24:44

这是我的解决办法:

代码语言:javascript
复制
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})

不是最优的,而是短的)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70374088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档