【问题标题】:How to iterate json consists of array elements如何迭代json由数组元素组成
【发布时间】:2022-01-10 20:46:26
【问题描述】:

我得到了这个对象:

"contributionData": {
        "Contribution 1": [
            4,
            4
        ],
        "Contribution 2": [
            1,
            1
        ]
    }

我想要迭代后的这种表。

contribution1 |  4  |  4  |
contribution2 |  1  |  1  |

我已将其转换为数组:

        let result = [];

        for(let i in this.state.testData) {
            result.push([i, this.state.testData[i]]);
        }

console log after converting it to array

   <tr>
       <td>{this.state.result}</td>
   </tr>

我想以表格形式填写这些数据 我正在研究 React js,我想在 javascript 中执行此操作。

任何帮助将不胜感激。

【问题讨论】:

  • 请展示你的尝试
  • 我看不出有什么不同,请发布您想要的 jon

标签: javascript arrays reactjs json iteration


【解决方案1】:

试试这个。

let result = []
let data = {
  "Contribution 1": [
            4,
            4
        ],
        "Contribution 2": [
            1,
            1
        ]
}
Object.keys(data).map((item) => {result.push([item].concat(data[item]))})
console.log(result);
...
{
    results.map(item, i)=> (
      <tr key={i}>
          {item.map(val, index) => (
            <td key={"row" + index}>{val}</td>
          )}
      </tr>
    )
}

【讨论】:

  • 如何在表格中填写这些数据?这个能不能写js的map函数
  • 我更新了答案。看看吧。
  • 谢谢,成功了
  • 这是我的荣幸。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 2013-11-21
  • 2017-12-27
  • 2011-04-06
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
相关资源
最近更新 更多