我使用 Laravel 作为后端,使用 jQuery 作为前端。我收到一个响应,它在主数组内的对象中有一个数组(希望有意义)。
这是我的对象的一部分:
{
"basket": [{
"id": 17,
"restaurant_id": 1,
"table_id": 1,
"item_id": 9,
"item_language_id": 37,
"price": "25.99",
"qty": 1,
"item": {
"id": 9,
"user_id": 1,
"restaurant_id": 1,
"category_id": 5,
"name": "Grilled Beef with potatoes",
"price": "25.99",
"short_description": "Meat / Potatoes / Rice / Tomatoe",
"itemlanguages": [{
"id": 37,
"user_id": 1,
"item_id": 9,
"lang_id": 9,
"restaurant_id": 1,
"category_id": 5,
"category_language_id": 21,
"name": "烤牛肉配土豆",
"price": "MVR 25.99",
"short_description": "肉/土豆/大米/西红柿",
"description": "<p>test</p>"
}]
}
}]
}
basket是一个数组。里面是一个item有itemlanguages数组的对象。我想通过迭代basket,但是显示器itemlaguages name,price不是从等item。这是我迄今为止尝试过的。
$.each(response.basket, function(index, val) {
$.each(this.item.itemlanguages, function(index, val) {
name = this.name;
short_description = this.short_description;
});
var name = null;
var short_description = null;
var qty = this.qty;
var price = this.price;
$('<span>' + name + ' | ' + short_description + ' | ' + qty + ' | ' + price + '</span>').appendTo(basket);
});
不过,我觉得我可以遍历如何我通常先这样做时each()就response.basket和它里面做this.item.itemlanguages,但没有奏效。我试图将值分配给一个变量。任何帮助表示赞赏。
慕姐8265434
随时随地看视频慕课网APP
相关分类