JavaScript convert json to array/map convert json array to java list of objects,convert json

My rest API returns with the following json content:

[{
    "key": "apple",
    "value": "green"
},
{
    "key": "banana",
    "value": "yellow"
}]

I am iterating through the list with this code:

this.props.json.map(row => {
    return <RowRender key={id.row} row={row} />
}

It works because the content is displayed but on the web browser's console I can see an error:

map is not a function

I googled for it and I changed my code to this:

Array.prototype.slice.call(this.props.json).map(row => {
    return <RowRender key={id.row} row={row} />
}

It works without any error but seems so complicated. Is that the correct way to do this job?


UPDATE

What I tried:

  • JSON.parse(...).map: Unexpected end of JSON input
  • JSON.parse(JSON.stringify(...)).map(...): data is displayed but I get an error: JSON.parse(...).map is not a function
  • Array(...).map: Each child in array or iterator should have a unique key.
Answer:1

in your code this.props.json is not an array, it's an array-like object and map is an array function. what your doing to solve this, is converting that array-like object into an array, with .slice()

a more elegant way to do this:

Array.from(this.props.json).map(row => <RowRender key={id.row} row={row} />)
Answer:2

I have found there a nice and working perfectly fine tutorial to play with music with a web library. Everything is working fine but I would like to have the "average value" of the music I play. Let ...

I have found there a nice and working perfectly fine tutorial to play with music with a web library. Everything is working fine but I would like to have the "average value" of the music I play. Let ...

I have a JSON object: var info=JSON.parse(server_response); I run console.log(info); And got this output: [ [ [ "Dipu", "Mondal", "O Positive", "017xxxx", "...

I have a JSON object: var info=JSON.parse(server_response); I run console.log(info); And got this output: [ [ [ "Dipu", "Mondal", "O Positive", "017xxxx", "...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. cancun
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis

I'm trying to make kind of typewriting effect in HTML5. For rendering and puting it all together , I'm using this pixi.js plugin. So far I have been able to make it work (partially), although it ...

I'm trying to make kind of typewriting effect in HTML5. For rendering and puting it all together , I'm using this pixi.js plugin. So far I have been able to make it work (partially), although it ...

The azure-functions-cli offers a way to kickoff debugging, but these instructions seem to be Visual Studio specific. I tried using a similar trick to serverless by setting up a run config in WebStorm ...

The azure-functions-cli offers a way to kickoff debugging, but these instructions seem to be Visual Studio specific. I tried using a similar trick to serverless by setting up a run config in WebStorm ...

  1. azure functions debug locally
  2. azure functions debug vscode
  3. azure functions debug visual studio
  4. azure functions debug port
  5. azure functions debug
  6. azure functions debug log
  7. azure functions debug console
  8. azure functions debug mode
  9. azure functions debug timertrigger
  10. azure functions debug https
  11. azure function debug remote
  12. azure function debug not working
  13. azure function debug blob trigger
  14. azure function debug locally timer trigger
  15. azure function debug change port
  16. azure function debug startup