反应在JSON中获取各种URL的每个URL
我有一个这样的JSON文件:
[
{
"name": "google",
"route": "/google",
"url": "www.google.com"
},
{
"name": "bing",
"route": "/bing",
"url": "www.bing.com"
},
{
"name": "duckduckgo",
"route": "/duckduckgo",
"url": "www.duckduckgo.com"
}
]
我想获取此JSON文件的每个URL,并在表中显示获取的状态。我的代码有效:
// Construct an object with the JSON data
const [data, setData] = useState(jsonData)
// We need to put the fetch call inside useEffect
// otherwise the fetchData will be called over and over every time the state is updated
useEffect(() => {
let headers = new Headers()
//headers.append('Content-Type', 'application/json')
//headers.append('Accept', 'application/json')
headers.append('Origin', 'http://localhost:8080')
// Array with all the promises from each fetch
// @r contains all the info about the fetch, we are going to use the r.status later
// @i is the index of the url in the JSON file
const promises = jsonData.map((url, i) => {
return fetch(url.route, {
mode: 'no-cors',
headers: headers,
}).then((r) => ({
fetch: r,
index: i,
}))
})
Promise.all(promises)
.then((result) => {
// Iterates through each promise and replaces the status value from the JSON file
// with the status value from the fetch
const new_data = result.map((d) => {
jsonData[d.index].status = d.fetch.status
return jsonData[d.index]
})
setData(new_data)
})
.catch((error) => {
console.log('Error: ', error)
})
}, [])
现在,我更新了我的JSON文件,它更复杂了:
[
{
"section": "Sonarqube",
"img": "sonarqube.png",
"urls": [
{
"name": "SonarQube",
"route": "/sonarqube",
"url": "https://sonarqube-enterprise.com"
},
{
"name": "SonarQube1",
"route": "/sonarqube",
"url": "https://sonarqube-enterprise1.com"
},
{
"name": "SonarQub2e",
"route": "/sonarqube",
"url": "https://sonarqube-enterprise2.com"
}
]
},
{
"section": "Twistlock",
"img": "twistlock.png",
"urls": [
{
"name": "Twistlock",
"route": "/twistlock",
"url": "https://twistlock.com"
}
]
}
]
我正在尝试更改代码以读取 urls 每个部分中的数组,但是我无法弄清楚如何使其起作用。你能帮我吗这就是我现在的位置:
const [data, setData] = useState(jsonData)
useEffect(() => {
const promises = jsonData.map((section, s) => {
return section.urls.map((url, i) => {
return fetch(url.route, {
mode: 'no-cors',
}).then((r) => ({
fetch: r,
index: i,
sectionId: s,
}))
})
})
Promise.all(promises)
.then((result) => {
const new_data = result.map((section) => {
section.map((d) => {
jsonData[d.sectionId].urls[d.index].status = d.fetch.status
jsonData[d.sectionId].urls[d.index].statusText = d.fetch.statusText
return jsonData[d.sectionId].urls[d.index]
})
})
setData(new_data)
})
.catch((error) => {
console.log('Error: ', error)
})
}, [])
我相信我快到了,但我真的不明白如何做这项工作:哦,你能帮我
I had a json file like this:
[
{
"name": "google",
"route": "/google",
"url": "www.google.com"
},
{
"name": "bing",
"route": "/bing",
"url": "www.bing.com"
},
{
"name": "duckduckgo",
"route": "/duckduckgo",
"url": "www.duckduckgo.com"
}
]
I wanted to fetch each url of this json file and show the status of the fetchs in a table. I had this code that worked:
// Construct an object with the JSON data
const [data, setData] = useState(jsonData)
// We need to put the fetch call inside useEffect
// otherwise the fetchData will be called over and over every time the state is updated
useEffect(() => {
let headers = new Headers()
//headers.append('Content-Type', 'application/json')
//headers.append('Accept', 'application/json')
headers.append('Origin', 'http://localhost:8080')
// Array with all the promises from each fetch
// @r contains all the info about the fetch, we are going to use the r.status later
// @i is the index of the url in the JSON file
const promises = jsonData.map((url, i) => {
return fetch(url.route, {
mode: 'no-cors',
headers: headers,
}).then((r) => ({
fetch: r,
index: i,
}))
})
Promise.all(promises)
.then((result) => {
// Iterates through each promise and replaces the status value from the JSON file
// with the status value from the fetch
const new_data = result.map((d) => {
jsonData[d.index].status = d.fetch.status
return jsonData[d.index]
})
setData(new_data)
})
.catch((error) => {
console.log('Error: ', error)
})
}, [])
Now, I updated a bit my json file, it is a little bit more complicated:
[
{
"section": "Sonarqube",
"img": "sonarqube.png",
"urls": [
{
"name": "SonarQube",
"route": "/sonarqube",
"url": "https://sonarqube-enterprise.com"
},
{
"name": "SonarQube1",
"route": "/sonarqube",
"url": "https://sonarqube-enterprise1.com"
},
{
"name": "SonarQub2e",
"route": "/sonarqube",
"url": "https://sonarqube-enterprise2.com"
}
]
},
{
"section": "Twistlock",
"img": "twistlock.png",
"urls": [
{
"name": "Twistlock",
"route": "/twistlock",
"url": "https://twistlock.com"
}
]
}
]
I'm trying to change my code to read the urls array inside each section but I can't figure out how to make it work. Can you please help me. This is where I am for now:
const [data, setData] = useState(jsonData)
useEffect(() => {
const promises = jsonData.map((section, s) => {
return section.urls.map((url, i) => {
return fetch(url.route, {
mode: 'no-cors',
}).then((r) => ({
fetch: r,
index: i,
sectionId: s,
}))
})
})
Promise.all(promises)
.then((result) => {
const new_data = result.map((section) => {
section.map((d) => {
jsonData[d.sectionId].urls[d.index].status = d.fetch.status
jsonData[d.sectionId].urls[d.index].statusText = d.fetch.statusText
return jsonData[d.sectionId].urls[d.index]
})
})
setData(new_data)
})
.catch((error) => {
console.log('Error: ', error)
})
}, [])
I believe that I'm almost there but I really don't understand how to make this work :O can you help me please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您返回嵌套地图中的数组或
jsondata.map
中的保证,您需要添加
.flat()
flatten array( https://develoder.mozilla.mozilla.org/en-us/en-us/docs/ddocs/web/javascript /参考/global_objects/array/flat )You return array of array in nested map nor promises in your
jsonData.map
You need to add
.flat()
to flatten array (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)尝试这样。您可以首先循环外部数组,然后做
Promises.All
。并破坏您想要的属性。Try like this. You can first loop the outer array and then do the
Promises.all
. And destructure the properties you want.我不知道为什么您会使事情复杂化,请尝试将代码分开,以便可以理解和容易阅读。
无论如何,这就是我的做法。
I don't know why you complicate things, try to separate the code so it could be understandable and easy for you to read.
Anyway here is how I would do it.