admin管理员组文章数量:1410682
Can someone help me to print out the required data on the web by parsing JSON array of objects? In the below example, I want to print Apple, Mango, Grapes.
Below are the codes:
JSON format
{
"data":{
"summary": {
'a1':13223,
'b1': 23
},
"regional": [
{
'aa1': "Apple",
'bb1': 456
},
{
'aa1': "Mango",
'bb1': 496
},
{
'aa1': "Grapes",
'bb1': 126
},
]
},
'lastdata': '2946'
}
fetchApi.js
url = 'apiUrl'
export const fetchData = async () => {
try {
const { data } = await (await fetch(url)).json();
const modifiedData = {
region: [...data.regional]
}
return modifiedData;
} catch (error) {
console.log(error)
}
}
tabular.js
import { fetchData } from "../api/fetchApi";
const Tabular = () => {
const [fetchedState, setFetchedState] = useState([])
useEffect(() => {
const fetchAPIState = async () => {
const dailyData = await fetchData()
setFetchedState(dailyData)
console.log(dailyData)
}
fetchAPIState()
}, [])
return (
<>
<h1>{}</h1>
</>
)
}
When I console.log(dailyData) the result is this:
"region": [
{
'aa1': "Apple",
'bb1': 456
},
{
'aa1': "Mango",
'bb1': 496
},
{
'aa1': "Grapes",
'bb1': 126
},
]
But I am unable to print Apple, Mango, Grapes in the h1 tag. It shows error. I tried using map method.
Can someone help me to print out the required data on the web by parsing JSON array of objects? In the below example, I want to print Apple, Mango, Grapes.
Below are the codes:
JSON format
{
"data":{
"summary": {
'a1':13223,
'b1': 23
},
"regional": [
{
'aa1': "Apple",
'bb1': 456
},
{
'aa1': "Mango",
'bb1': 496
},
{
'aa1': "Grapes",
'bb1': 126
},
]
},
'lastdata': '2946'
}
fetchApi.js
url = 'apiUrl'
export const fetchData = async () => {
try {
const { data } = await (await fetch(url)).json();
const modifiedData = {
region: [...data.regional]
}
return modifiedData;
} catch (error) {
console.log(error)
}
}
tabular.js
import { fetchData } from "../api/fetchApi";
const Tabular = () => {
const [fetchedState, setFetchedState] = useState([])
useEffect(() => {
const fetchAPIState = async () => {
const dailyData = await fetchData()
setFetchedState(dailyData)
console.log(dailyData)
}
fetchAPIState()
}, [])
return (
<>
<h1>{}</h1>
</>
)
}
When I console.log(dailyData) the result is this:
"region": [
{
'aa1': "Apple",
'bb1': 456
},
{
'aa1': "Mango",
'bb1': 496
},
{
'aa1': "Grapes",
'bb1': 126
},
]
But I am unable to print Apple, Mango, Grapes in the h1 tag. It shows error. I tried using map method.
Share edited Apr 15, 2020 at 8:06 kaizen asked Apr 15, 2020 at 5:48 kaizenkaizen 1,2101 gold badge17 silver badges21 bronze badges2 Answers
Reset to default 1In fetchApi.js, make
modifiedData = [...data.regional];
Or, in tabular.js, you need to use map
on fetchedState.region
In the jsx part(assuming you did the latter), if you give the same key to all the fruit names, and bb1 is a unique id, replace the <h1>{}</h1>
with:
{fetchedState.region.map(e => <h1 key={e.bb1}>{e.aa1}</h1>)}
So, from your question what I understand that you want to render a list of elements from your dailyData
array, right? If that's the case then we can do that by using javascript map()
on the fetchedState
(because you're saving dailyData
array to that state) and generate the list of elements. Finally, we can render that list of elements in the JSX
side. Or, you can directly do the transforming i.e. map()
part in the JSX
side.
But the dailyData
array of objects you gave has not a consistent shape. For creating an element for each Apple, Mango, Grapes in the h1
tag you must have a data/array where these values are in the same key/property
; here I can see Apple, Mango, Grapes have 3 different key
i.e. aa1
, cc1
, ee1
!! So, are you sure that the data points you are showing are correct?
本文标签: javascriptHow to parse Json arrays of objects in React JSStack Overflow
版权声明:本文标题:javascript - How to parse Json arrays of objects in React JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744950193a2634035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论