Once my REST API responds, it provides the following JSON content:
[{
"key": "apple",
"value": "green"
},
{
"key": "banana",
"value": "yellow"
}]
Managing the data, I use the following code to iterate through the list:
this.props.json.map(row => {
return <RowRender key={id.row} row={row} />
}
While the content displays correctly, an error appears in the web browser console:
map is not a function
To address this, I searched online and altered my code as follows:
Array.prototype.slice.call(this.props.json).map(row => {
return <RowRender key={id.row} row={row} />
}
Although this resolves the error, the solution appears complicated. Is this the correct approach for this task?
UPDATE
My attempts so far:
- Using JSON.parse(...).map: I encounter an "Unexpected end of JSON input" error
- Using JSON.parse(JSON.stringify(...)).map(...): The data displays but I receive an error: "JSON.parse(...).map is not a function"
- Using Array(...).map: I face the issue that each child in array or iterator should have a unique key.