Skip to content
Advertisement

How to iterate json consists of array elements

I am getting this object :

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

I want this kind of table after iteration.

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

I’ve converted it to array :

        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>

I want to fill this data in table format I am working on React js, I want to do this in javascript.

Any help would be appreciated.

Advertisement

Answer

Try this one.

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>
    )
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement