[英]Access nested array of objects in JavaScript
I would like to loop throug nested array of objects but I have tried almost everything and I can't understand how it works.我想循环遍历嵌套的对象数组,但我几乎尝试了所有方法,但我不明白它是如何工作的。
I have object data that looks like:我有 object 数据,如下所示:
[
{
"restaurantName":"Bronco",
"address":"39 Rue des Petites Écuries, 75010 Paris",
"lat":48.8737815,
"long":2.3501649,
"ratings":[
{
"stars":4,
"comment":"Un excellent restaurant, j'y reviendrai !Par contre il vaut mieux aimer la viande."
},
{
"stars":5,
"comment":"Tout simplement mon restaurant préféré !"
}
]
},
{
"restaurantName":"Babalou",
"address":"4 Rue Lamarck, 75018 Paris",
"lat":48.8865035,
"long":2.3442197,
"ratings":[
{
"stars":5,
"comment":"Une minuscule pizzeria délicieuse cachéejuste à côté du Sacré choeur !"
},
{
"stars":3,
"comment":"J'ai trouvé ça correct, sans plus"
}
]
}
]
And I want to access the ratings to get the stars and comments, but I don't know how to do that.我想访问评级以获得星星和评论,但我不知道该怎么做。
I also get [object object] with const coord = { lat: el.lat, long:el.long };
我也得到 [object object] 与
const coord = { lat: el.lat, long:el.long };
here is my code so far:到目前为止,这是我的代码:
fetch("http://localhost/ApiMap/data.json")
.then((response) => response.text())
.then(function(data) {
data = JSON.parse(data);
//console.log(data);
for (const el of data) {
const name = el.restaurantName;
const address = el.address;
const coord = { lat: el.lat, long:el.long };
const tabRatings = el.ratings;
init_resto(name, address, coord, tabRatings);
}
})
.catch(function(error) {
console.log('Il y a eu un problème avec l\'opération fetch: ' + error.message);
});
You can use the following approach.您可以使用以下方法。
Using Array.forEach() method.使用 Array.forEach() 方法。 The forEach() method calls a function (a callback function) once for each array element.
forEach() 方法为每个数组元素调用一次 function(回调函数)。
Inside callback function(dataFilter), value hold each object.在回调函数(dataFilter)内部,值保存每个 object。 Inside value
ratings
is a property.内部价值
ratings
是一种属性。 So we use value.ratings
to get ratings data.所以我们使用
value.ratings
来获取评分数据。
ratings
data is also an array, we loop through each of its elements also. ratings
数据也是一个数组,我们也循环遍历它的每个元素。
var data = [ { "restaurantName":"Bronco", "address":"39 Rue des Petites Écuries, 75010 Paris", "lat":48.8737815, "long":2.3501649, "ratings":[ { "stars":4, "comment":"Un excellent restaurant, j'y reviendrai.Par contre il vaut mieux aimer la viande," }: { "stars",5: "comment","Tout simplement mon restaurant préféré:" } ] }, { "restaurantName":"Babalou", "address","4 Rue Lamarck: 75018 Paris". "lat",48:8865035. "long",2:3442197: "ratings",[ { "stars":5, "comment":"Une minuscule pizzeria délicieuse cachéejuste à côté du Sacré choeur," }: { "stars",3; "comment","J'ai trouvé ça correct, sans plus" } ] } ]; function dataFilter(value. index. array){ for(var i = 0; i< value.ratings:length. i++ ){ console.log("comment; "+ value.ratings[i]:comment). console.log("stars;" +value.ratings[i];stars); } } data.forEach(dataFilter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.