【发布时间】:2022-01-06 06:10:27
【问题描述】:
这是有问题的数据:
[
{
"id": 3,
"owner": "http://localhost:8000/api/v1/users/3/",
"ingredients": [
{
"ingredient": {
"id": 1,
"category": "Fruit",
"name": "Apple",
"calories": 100.0,
},
"numOf": 4
},
{
"ingredient": {
"id": 2,
"category": "Vegetable",
"name": "Potato",
"calories": 10.0,
},
"numOf": 3
}
],
"total_number": 0
}
]
我想将 numOf 连接到 hist parent ingredient 以便我的表正确呈现现在给定 2 种成分,我的表有 4 行
编辑
想要的结果应该是这样的:
[
{
"id": 3,
"owner": "http://localhost:8000/api/v1/users/3/",
"ingredients": [
{
"ingredient": {
"id": 1,
"category": "Fruit",
"name": "Apple",
"calories": 100.0,
"numOf": 4,
}
},
{
"ingredient": {
"id": 2,
"category": "Vegetable",
"name": "Potato",
"calories": 10.0,
"numOf": 3,
}
}
],
"total_number": 0
}
]
您看到的 JSON 文件是我在查询用户冰箱时的服务器响应。然后我想在一张桌子上显示他所有的成分。 以下是我在 VueJS 中声明数组的方式:
import axios from 'axios'
export default {
name: 'Fridge',
data() {
return {
username: '',
fridge: {},
ingredients: []
}
},
}
这是循环响应数据并添加ingredients 对象并将其存储在名为ingredients 的数组中的代码。我试过声明是一个对象,但没有成功。
for (let i = 0; i < res.data["ingredients"].length; i++) {
var obj = this.fridge.ingredients[i]
for (var key in obj) {
var value = obj[key]
this.ingredients.push(value)
}
}
【问题讨论】:
-
您能告诉我们想要的结果吗?描述本身还不够清楚。到目前为止,您自己尝试过什么?
标签: javascript arrays json for-loop html-table