Displayed below is a snippet of data stored in the data.json
file:
[
{"id": 23, "name": "Good!", "state": "OK"},
{"id": 24, "name": "Not good...", "state": "Fail"},
{"id": 26, "name": "Oh...", "state": "OK"},
{"id": 27, "name": "What?", "state": "Fail"}
]
Utilizing a script below, an attempt is made to map the data:
import * as data from './data.json'
let jsonData = data
console.log(jsonData)
jsonData = jsonData.map(({name, state}) => ({name, state}))
console.log(jsonData)
Displayed below is the output:
{default: Array(4)}
default
:
(4) [{...}, {...}, {...}, {...}]
0
:
(3) {id: 23, name: "Good!", state: "OK"}
1
:
(3) {id: 24, name: "Not good...", state:...}
2
:
(3) {id: 26, name: "Oh...", state: "OK"}
3
:
(3) {id: 27, name: "What?", state: "Fail...}
TypeError: jsonData.map is not a function
at <anonymous>:35:21
at dn (<anonymous>:16:5449)
I am perplexed as the first console.log()
call appears to output something iterable. Why then does it result in a TypeError
?
I have attempted various solutions like:
let jsonData = JSON.parse(JSON.stringify(data))
but none have proven to be successful.
What steps should be taken to parse the data from the file in order to be compatible with the map()
method?