Javascript 在JSON中动态呈现对象
给定此javascript文件:Javascript 在JSON中动态呈现对象,javascript,json,axios,fetch,Javascript,Json,Axios,Fetch,给定此javascript文件: locations: { "0": { name: "Paris", lat: "48.866666670", lng: "2.333333333", color: "default", description: "default", url: "default" }, "1": { name: "Tokyo", lat: "35.666666670", lng: "139.750000000", color: "defa
locations: {
"0": {
name: "Paris",
lat: "48.866666670",
lng: "2.333333333",
color: "default",
description: "default",
url: "default"
},
"1": {
name: "Tokyo",
lat: "35.666666670",
lng: "139.750000000",
color: "default",
description: "default",
url: "default"
},
"3": {
name: "New York",
lat: "40.71",
lng: "-74.0059731",
description: "default",
color: "default",
url: "default",
size: "default"
},
"4": {
name: "Miami",
lat: "25.76",
lng: "-80.19",
description: "default",
color: "blue",
url: "default",
size: "default"
},
"5": {
name: "Los Angeles",
lat: "34.0522",
lng: "-118.2437",
description: "<h5 style='color:blue;font-weight:900;'>Aircraft Carrier:</h5><p>American Airlines</p>",
color: "blue",
url: "default",
size: "default"
},
"6": {
name: "Robinson Helicopter R44",
lat: "25.7090",
lng: "-80.4381",
description: "<h5 style='color:blue;font-weight:900;'>Aircraft Carrier:</h5><p style='font-size:.9rem'>American Airlines</p>",
color: "blue",
url: "default",
size: "default"
},
"7": {
name: "Robinson Helicopter R44",
lat: "40.4839361",
lng: "-3.5679515",
description: "<h5 style='color:blue;font-weight:900;'>Aircraft Carrier:</h5><p style='font-size:.9rem'>American Airlines</p>",
color: "blue",
url: "default",
size: "default"
},
可以将内部对象转换为数组,这样更易于渲染
const response = {"4": {...}, "5": {...}};
const cities = Object.keys(response).map((city) => response[city]);
像这样的
fetch('https://www.podhurst.com/wp-json/wp/v2/aviation_map/')
.then(res=>res.json())
.then(res=>console.log({ ...res }))
你会怎样对待你的孩子?通过单击父对象,然后调用具有该id的端点(父对象)?不确定渲染子对象是什么意思,但@izambl solution会将其展平为一个数组。@EugenUnic因此端点将为您提供所有信息,如名称long&latitude。最终结果需要像“index”一样返回:{name:${fromEndPoint},lat:${fromEndPoint}}这收到了一个语法错误:SyntaxError:缺少形式参数我不确定详细说明如果将代码复制并粘贴到控制台中,它会工作
fetch('https://www.podhurst.com/wp-json/wp/v2/aviation_map/')
.then(res=>res.json())
.then(res=>console.log({ ...res }))