【问题标题】:How to handle a nested json array to show in a table?如何处理嵌套的 json 数组以显示在表格中?
【发布时间】:2020-10-15 04:01:09
【问题描述】:

我有这个 JSON 数组。我在表格中显示数据。那么,我应该如何将 'transactionData' 放在主对象中?即我希望对象为

{
completeTime: "value",
createTime: "value2",
assigneeName:"value3",
assigneeMap:"value4"
}

由于这是一个 JSON 数组,所以我需要一种方法来迭代数组并根据需要制作每个对象。

我不能使用原始的 JSON 数组,因为对象 transactionData 不是固定的,它的键可能会改变。因此,我不想硬编码任何值,例如 assigneeMap 或 assigneeName,因为它可能会改变。 因此,我想要 transactionData 对象中的任何值,我想将它插入到我的主对象中。

【问题讨论】:

标签: javascript arrays json vue.js object


【解决方案1】:

使用array.map(),类似这样的

results.map(x=>{
{
completeTime: x.completeTime,
createTime: x.createTime,
assigneeName: x.assigneeName || x.assigneename,
assigneeMap:x.assigneeMap || x.assigneeMap || x.yourChangedKey,
}
})

【讨论】:

    【解决方案2】:

    使用 Array.prototype.map()

    const result = arr.map(item => {
        const { completeTime, createTime, data: { transactionData } } = item;
        const { assigneeName, assigneeMap } = transactionData;
        return {
            completeTime,
            createTime,
            assigneeName,
            assigneeMap
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 2018-10-28
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多