循环遍历包含更多对象和数组的 JSON 对象



我有一个以下形式的小行星数据的JSON对象:

{
"links" : {
"next" : "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
"prev" : "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
"self" : "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"element_count" : 40,
"near_earth_objects" : {
"2018-06-09" : [ {
"links" : {
"self" : "https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id" : "2162474",
"name" : "162474 (2000 LB16)",
"nasa_jpl_url" : "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474",
"absolute_magnitude_h" : 18.5,
"estimated_diameter" : {
"kilometers" : {
"estimated_diameter_min" : 0.5303407233,
"estimated_diameter_max" : 1.1858779086
},
"meters" : {
"estimated_diameter_min" : 530.3407233187,
"estimated_diameter_max" : 1185.8779085771
},
"miles" : {
"estimated_diameter_min" : 0.3295383456,
"estimated_diameter_max" : 0.7368701419
},
"feet" : {
"estimated_diameter_min" : 1739.963058693,
"estimated_diameter_max" : 3890.675677576
}
},
"is_potentially_hazardous_asteroid" : true,
"close_approach_data" : [ {
"close_approach_date" : "2018-06-09",
"epoch_date_close_approach" : 1528527600000,
"relative_velocity" : {
"kilometers_per_second" : "29.5006815383",
"kilometers_per_hour" : "106202.4535379664",
"miles_per_hour" : "65990.074533077"
},
"miss_distance" : {
"astronomical" : "0.2778177136",
"lunar" : "108.0710983276",
"kilometers" : "41560940",
"miles" : "25824772"
},
"orbiting_body" : "Earth"
} ]
"2018-06-07" : [...
]

我正在尝试遍历它,仅访问与今天日期"2018-06-07"相关的对象数组中每颗小行星的名称,并在 html 页面中显示每颗小行星的名称

这是我到目前为止尝试过的:

// get JSON data for todays asteroids
$.getJSON("https://api.nasa.gov/neo/rest/v1/feed?start_date="+today+"&api_key="+API_KEY, function(result) {

// iterate over all keys
for (let x in result) {
// if key is 'near_earth_objects'
if (x == 'near_earth_objects') {
// iterate over dates in 'near_earth_objects'
for (y in result.near_earth_objects) {
// if todays date
if (y == result.near_earth_objects[today]) {
// loop over array of objects for todays date
for (let i=0; i<result.near_earth_objects[today].length; i++) {

let id = 'n'+(i+1);
let output = document.getElementById(id);
output.innerhtml = result.near_earth_objects[today][i].name;
}
}
}
}
}

但是我的 htmldiv 显示为空白。有什么想法吗?

附言"今天"是我已经定义为今天的日期,格式正确为"YYYY-MM-DD">

我不确定为什么要使用循环来选择属性。您可以直接选择正确的阵列。然后循环访问其元素并将它们追加到文档中。

我从 nasa.gov 中获取了一些数据来向您展示:

result.near_earth_objects["2018-06-18"].forEach(object => {
const el = document.createElement('div')
el.innerHTML = object.name
document.querySelector('body').appendChild(el)
});
<script>
const result = {  
"links":{  
"next":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-21&end_date=2018-06-28&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
"prev":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
"self":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"element_count":38,
"near_earth_objects":{
"2018-06-18":[  
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3825034?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3825034",
"name":"(2018 KR2)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3825034",
"absolute_magnitude_h":19.136,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.3956899106,
"estimated_diameter_max":0.884789538
},
"meters":{  
"estimated_diameter_min":395.6899105531,
"estimated_diameter_max":884.7895380075
},
"miles":{  
"estimated_diameter_min":0.2458702354,
"estimated_diameter_max":0.54978256
},
"feet":{  
"estimated_diameter_min":1298.1952861389,
"estimated_diameter_max":2902.8529078765
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-18",
"epoch_date_close_approach":1529305200000,
"relative_velocity":{  
"kilometers_per_second":"12.0438743406",
"kilometers_per_hour":"43357.9476263051",
"miles_per_hour":"26940.9425125783"
},
"miss_distance":{  
"astronomical":"0.3064664713",
"lunar":"119.2154541016",
"kilometers":"45846732",
"miles":"28487838"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3563152?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3563152",
"name":"(2011 GA62)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3563152",
"absolute_magnitude_h":18.9,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.4411182,
"estimated_diameter_max":0.9863702813
},
"meters":{  
"estimated_diameter_min":441.1181999969,
"estimated_diameter_max":986.3702813054
},
"miles":{  
"estimated_diameter_min":0.2740980571,
"estimated_diameter_max":0.6129018881
},
"feet":{  
"estimated_diameter_min":1447.2382352778,
"estimated_diameter_max":3236.1230737181
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-18",
"epoch_date_close_approach":1529305200000,
"relative_velocity":{  
"kilometers_per_second":"7.9455926818",
"kilometers_per_hour":"28604.1336544372",
"miles_per_hour":"17773.4962698918"
},
"miss_distance":{  
"astronomical":"0.1550728286",
"lunar":"60.3233299255",
"kilometers":"23198564",
"miles":"14414920"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3733265?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3733265",
"name":"(2015 VD2)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3733265",
"absolute_magnitude_h":24.6,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.0319561887,
"estimated_diameter_max":0.0714562102
},
"meters":{  
"estimated_diameter_min":31.9561886721,
"estimated_diameter_max":71.4562101727
},
"miles":{  
"estimated_diameter_min":0.0198566489,
"estimated_diameter_max":0.0444008168
},
"feet":{  
"estimated_diameter_min":104.8431420431,
"estimated_diameter_max":234.436392583
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-18",
"epoch_date_close_approach":1529305200000,
"relative_velocity":{  
"kilometers_per_second":"16.5811851502",
"kilometers_per_hour":"59692.2665406091",
"miles_per_hour":"37090.4530624135"
},
"miss_distance":{  
"astronomical":"0.3445160884",
"lunar":"134.0167694092",
"kilometers":"51538876",
"miles":"32024772"
},
"orbiting_body":"Earth"
}
]
}
],
"2018-06-17":[  
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/2162168?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"2162168",
"name":"162168 (1999 GT6)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162168",
"absolute_magnitude_h":17.0,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":1.0581688593,
"estimated_diameter_max":2.3661375011
},
"meters":{  
"estimated_diameter_min":1058.1688593312,
"estimated_diameter_max":2366.137501138
},
"miles":{  
"estimated_diameter_min":0.6575154423,
"estimated_diameter_max":1.4702492252
},
"feet":{  
"estimated_diameter_min":3471.6827204482,
"estimated_diameter_max":7762.9185592335
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-17",
"epoch_date_close_approach":1529218800000,
"relative_velocity":{  
"kilometers_per_second":"9.6198699215",
"kilometers_per_hour":"34631.5317173891",
"miles_per_hour":"21518.6870273966"
},
"miss_distance":{  
"astronomical":"0.2630390442",
"lunar":"102.3221893311",
"kilometers":"39350084",
"miles":"24451008"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3785933?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3785933",
"name":"(2017 UA)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3785933",
"absolute_magnitude_h":28.5,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.0053034072,
"estimated_diameter_max":0.0118587791
},
"meters":{  
"estimated_diameter_min":5.3034072332,
"estimated_diameter_max":11.8587790858
},
"miles":{  
"estimated_diameter_min":0.0032953835,
"estimated_diameter_max":0.0073687014
},
"feet":{  
"estimated_diameter_min":17.3996305869,
"estimated_diameter_max":38.9067567758
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-17",
"epoch_date_close_approach":1529218800000,
"relative_velocity":{  
"kilometers_per_second":"7.3524855265",
"kilometers_per_hour":"26468.94789533",
"miles_per_hour":"16446.7748741844"
},
"miss_distance":{  
"astronomical":"0.3811914985",
"lunar":"148.283493042",
"kilometers":"57025436",
"miles":"35433964"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3801999?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3801999",
"name":"(2018 FS1)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3801999",
"absolute_magnitude_h":20.8,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.1838886721,
"estimated_diameter_max":0.411187571
},
"meters":{  
"estimated_diameter_min":183.8886720703,
"estimated_diameter_max":411.1875710413
},
"miles":{  
"estimated_diameter_min":0.1142630881,
"estimated_diameter_max":0.2555000322
},
"feet":{  
"estimated_diameter_min":603.309310875,
"estimated_diameter_max":1349.040630575
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-17",
"epoch_date_close_approach":1529218800000,
"relative_velocity":{  
"kilometers_per_second":"11.6654375478",
"kilometers_per_hour":"41995.5751720393",
"miles_per_hour":"26094.4172506485"
},
"miss_distance":{  
"astronomical":"0.3818340382",
"lunar":"148.5334320068",
"kilometers":"57121560",
"miles":"35493692"
},
"orbiting_body":"Earth"
}
]
}
],
"2018-06-16":[  
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3279436?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3279436",
"name":"(2005 LU3)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3279436",
"absolute_magnitude_h":26.2,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.0152951935,
"estimated_diameter_max":0.0342010925
},
"meters":{  
"estimated_diameter_min":15.2951935344,
"estimated_diameter_max":34.201092472
},
"miles":{  
"estimated_diameter_min":0.0095039897,
"estimated_diameter_max":0.021251567
},
"feet":{  
"estimated_diameter_min":50.1810827555,
"estimated_diameter_max":112.2083122258
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"16.9895489303",
"kilometers_per_hour":"61162.3761489486",
"miles_per_hour":"38003.9220021064"
},
"miss_distance":{  
"astronomical":"0.2620137139",
"lunar":"101.9233322144",
"kilometers":"39196692",
"miles":"24355696"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3283835?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3283835",
"name":"(2005 NW44)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3283835",
"absolute_magnitude_h":20.4,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.2210828104,
"estimated_diameter_max":0.4943561926
},
"meters":{  
"estimated_diameter_min":221.0828103591,
"estimated_diameter_max":494.3561926196
},
"miles":{  
"estimated_diameter_min":0.137374447,
"estimated_diameter_max":0.3071786018
},
"feet":{  
"estimated_diameter_min":725.3373275385,
"estimated_diameter_max":1621.9035709942
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"12.0629690223",
"kilometers_per_hour":"43426.6884801364",
"miles_per_hour":"26983.6554058936"
},
"miss_distance":{  
"astronomical":"0.0548729113",
"lunar":"21.3455619812",
"kilometers":"8208871",
"miles":"5100756"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3395958?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3395958",
"name":"(2007 WP3)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3395958",
"absolute_magnitude_h":23.5,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.0530340723,
"estimated_diameter_max":0.1185877909
},
"meters":{  
"estimated_diameter_min":53.0340723319,
"estimated_diameter_max":118.5877908577
},
"miles":{  
"estimated_diameter_min":0.0329538346,
"estimated_diameter_max":0.0736870142
},
"feet":{  
"estimated_diameter_min":173.9963058693,
"estimated_diameter_max":389.0675677576
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"15.7057349157",
"kilometers_per_hour":"56540.6456964908",
"miles_per_hour":"35132.1584329112"
},
"miss_distance":{  
"astronomical":"0.1746641507",
"lunar":"67.9443588257",
"kilometers":"26129386",
"miles":"16236048"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3429684?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3429684",
"name":"(2008 TZ)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3429684",
"absolute_magnitude_h":25.4,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.022108281,
"estimated_diameter_max":0.0494356193
},
"meters":{  
"estimated_diameter_min":22.1082810359,
"estimated_diameter_max":49.435619262
},
"miles":{  
"estimated_diameter_min":0.0137374447,
"estimated_diameter_max":0.0307178602
},
"feet":{  
"estimated_diameter_min":72.5337327539,
"estimated_diameter_max":162.1903570994
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"17.3250625132",
"kilometers_per_hour":"62370.2250473537",
"miles_per_hour":"38754.4323356737"
},
"miss_distance":{  
"astronomical":"0.1975837242",
"lunar":"76.8600692749",
"kilometers":"29558104",
"miles":"18366554"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3667048?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3667048",
"name":"(2014 GQ17)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3667048",
"absolute_magnitude_h":27.1,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.0101054342,
"estimated_diameter_max":0.0225964377
},
"meters":{  
"estimated_diameter_min":10.1054341542,
"estimated_diameter_max":22.5964377109
},
"miles":{  
"estimated_diameter_min":0.0062792237,
"estimated_diameter_max":0.0140407711
},
"feet":{  
"estimated_diameter_min":33.1543125905,
"estimated_diameter_max":74.1352966996
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"8.1155789012",
"kilometers_per_hour":"29216.0840443903",
"miles_per_hour":"18153.738444138"
},
"miss_distance":{  
"astronomical":"0.0855466094",
"lunar":"33.277633667",
"kilometers":"12797591",
"miles":"7952054.5"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/2469737?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"2469737",
"name":"469737 (2005 NW44)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2469737",
"absolute_magnitude_h":20.4,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.2210828104,
"estimated_diameter_max":0.4943561926
},
"meters":{  
"estimated_diameter_min":221.0828103591,
"estimated_diameter_max":494.3561926196
},
"miles":{  
"estimated_diameter_min":0.137374447,
"estimated_diameter_max":0.3071786018
},
"feet":{  
"estimated_diameter_min":725.3373275385,
"estimated_diameter_max":1621.9035709942
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"12.0629682412",
"kilometers_per_hour":"43426.685668464",
"miles_per_hour":"26983.6536588298"
},
"miss_distance":{  
"astronomical":"0.0548729224",
"lunar":"21.3455677032",
"kilometers":"8208872.5",
"miles":"5100757"
},
"orbiting_body":"Earth"
}
]
},
{  
"links":{  
"self":"https://api.nasa.gov/neo/rest/v1/neo/3797700?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id":"3797700",
"name":"(2018 BC)",
"nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3797700",
"absolute_magnitude_h":29.4,
"estimated_diameter":{  
"kilometers":{  
"estimated_diameter_min":0.0035039264,
"estimated_diameter_max":0.0078350176
},
"meters":{  
"estimated_diameter_min":3.5039264111,
"estimated_diameter_max":7.8350176433
},
"miles":{  
"estimated_diameter_min":0.0021772383,
"estimated_diameter_max":0.0048684527
},
"feet":{  
"estimated_diameter_min":11.4958219265,
"estimated_diameter_max":25.705439285
}
},
"is_potentially_hazardous_asteroid":false,
"close_approach_data":[  
{  
"close_approach_date":"2018-06-16",
"epoch_date_close_approach":1529132400000,
"relative_velocity":{  
"kilometers_per_second":"1.8772738596",
"kilometers_per_hour":"6758.1858945152",
"miles_per_hour":"4199.2738965114"
},
"miss_distance":{  
"astronomical":"0.0713602174",
"lunar":"27.7591247559",
"kilometers":"10675336",
"miles":"6633346.5"
},
"orbiting_body":"Earth"
}
]
}
]
}
}
</script>

尝试以下

var day = "2018-06-09";
var result = {"links":{"next":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS","prev":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS","self":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"},"element_count":40,"near_earth_objects":{"2018-06-09":[{"links":{"self":"https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"},"neo_reference_id":"2162474","name":"162474 (2000 LB16)","nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474","absolute_magnitude_h":18.5,"estimated_diameter":{"kilometers":{"estimated_diameter_min":0.5303407233,"estimated_diameter_max":1.1858779086},"meters":{"estimated_diameter_min":530.3407233187,"estimated_diameter_max":1185.8779085771},"miles":{"estimated_diameter_min":0.3295383456,"estimated_diameter_max":0.7368701419},"feet":{"estimated_diameter_min":1739.963058693,"estimated_diameter_max":3890.675677576}},"is_potentially_hazardous_asteroid":true,"close_approach_data":[{"close_approach_date":"2018-06-09","epoch_date_close_approach":1528527600000,"relative_velocity":{"kilometers_per_second":"29.5006815383","kilometers_per_hour":"106202.4535379664","miles_per_hour":"65990.074533077"},"miss_distance":{"astronomical":"0.2778177136","lunar":"108.0710983276","kilometers":"41560940","miles":"25824772"},"orbiting_body":"Earth"}]}]}};
result.near_earth_objects[day].forEach(({name}, i) => document.getElementById('n'+(i+1)).innerHTML = name);
<div id="n1"></div>

此外,您可以像以下方式改进代码

var day = "2018-06-09";
var result = {"links":{"next":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS","prev":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS","self":"https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"},"element_count":40,"near_earth_objects":{"2018-06-09":[{"links":{"self":"https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"},"neo_reference_id":"2162474","name":"162474 (2000 LB16)","nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474","absolute_magnitude_h":18.5,"estimated_diameter":{"kilometers":{"estimated_diameter_min":0.5303407233,"estimated_diameter_max":1.1858779086},"meters":{"estimated_diameter_min":530.3407233187,"estimated_diameter_max":1185.8779085771},"miles":{"estimated_diameter_min":0.3295383456,"estimated_diameter_max":0.7368701419},"feet":{"estimated_diameter_min":1739.963058693,"estimated_diameter_max":3890.675677576}},"is_potentially_hazardous_asteroid":true,"close_approach_data":[{"close_approach_date":"2018-06-09","epoch_date_close_approach":1528527600000,"relative_velocity":{"kilometers_per_second":"29.5006815383","kilometers_per_hour":"106202.4535379664","miles_per_hour":"65990.074533077"},"miss_distance":{"astronomical":"0.2778177136","lunar":"108.0710983276","kilometers":"41560940","miles":"25824772"},"orbiting_body":"Earth"}]}]}};
let asteroids = result.near_earth_objects[day];
for (let i = 0; i < asteroids.length; i++) {
document.getElementById('n'+(i+1)).innerHTML = asteroids[i].name;
}
<div id="n1"></div>

谢谢大家。是的,事实证明我只需要做:

for (let i=0; i<result.near_earth_objects[today].length; i++) {
let id = 'n'+(i+1);
let output = document.getElementById(id);
output.innerHTML = result.near_earth_objects[today][i].name;
}

感谢strah output.innerhtml也导致了错误...

我不得不伪造 api 调用,我没有密钥。我看到的一件事是我需要将y与实际日期进行比较。这可能只是我嘲笑返回结果的结果。然后,正如您所看到的,必须对 innerHTML 进行大小写更正。FWIW,这是一个工作示例。

<html lang="en">
<head>
<meta charset="utf-8">
<title>Asteroids</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body> 
<div id="n1"></div> 
<script>
var today = "2018-06-09";
var result = {
"links": {
"next": "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-14&end_date=2018-06-21&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
"prev": "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-05-31&end_date=2018-06-07&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS",
"self": "https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-06-07&end_date=2018-06-14&detailed=false&api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"element_count": 40,
"near_earth_objects": {
"2018-06-09": [{
"links": {
"self": "https://api.nasa.gov/neo/rest/v1/neo/2162474?api_key=IJtQgGp7nIbexsC9MZm9R3nYPtS2H51ykAQ5PrHS"
},
"neo_reference_id": "2162474",
"name": "162474 (2000 LB16)",
"nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2162474",
"absolute_magnitude_h": 18.5,
"estimated_diameter": {
"kilometers": {
"estimated_diameter_min": 0.5303407233,
"estimated_diameter_max": 1.1858779086
},
"meters": {
"estimated_diameter_min": 530.3407233187,
"estimated_diameter_max": 1185.8779085771
},
"miles": {
"estimated_diameter_min": 0.3295383456,
"estimated_diameter_max": 0.7368701419
},
"feet": {
"estimated_diameter_min": 1739.963058693,
"estimated_diameter_max": 3890.675677576
}
},
"is_potentially_hazardous_asteroid": true,
"close_approach_data": [{
"close_approach_date": "2018-06-09",
"epoch_date_close_approach": 1528527600000,
"relative_velocity": {
"kilometers_per_second": "29.5006815383",
"kilometers_per_hour": "106202.4535379664",
"miles_per_hour": "65990.074533077"
},
"miss_distance": {
"astronomical": "0.2778177136",
"lunar": "108.0710983276",
"kilometers": "41560940",
"miles": "25824772"
},
"orbiting_body": "Earth"
}]
}]
}
};
(function () { 
// iterate over all keys
for (let x in result) { 
// if key is 'near_earth_objects'
if (x == 'near_earth_objects') { 
// iterate over dates in 'near_earth_objects'
for (y in result.near_earth_objects) { 
// if todays date
if (y == today) { //Check y with today's value
// loop over array of objects for todays date
for (let i = 0; i < result.near_earth_objects[today].length; i++) {

let id = 'n' + (i + 1);
let output = document.getElementById(id);
output.innerHTML = result.near_earth_objects[today][i].name;
}
}
}
}
}
})();
</script> 
</body>
</html>

最新更新