我想添加像这样的数据
类别1
类别2
我的代码
getlist() {
var list = this.lists;
var category
// var company
this.$http.get("/getlist")
.then((res) => {
var obj = res.data;
for(var [key] in obj) {
var company =[];
for(var i in obj[key].company) {
company.push( obj[key].company[i].name)
}
console.log(company);
list.push({
"category_name" : obj[key].name,
"companies": [
{name: company}
]
})
list.category_name = '',
list.companies = '',
company= ''
}
})
},
列表表格看起来像这样
{
category_name: 'Category1',
companies: [
{name: 'company1'},
]
},
,数据看起来像这样
[
{
"name": "Category2",
"company": [
{
"name": "company1"
}
{
"name": "company2"
}
]
}
{
"name": "Category2",
"company": [
{
"name": "company1"
}
{
"name": "company2"
}
]
}
]
可以在list.push()中使用double forloop吗?
ㅠㅠ对我来说太累了..
你的帮助将很幸运ㅠㅠ
答案 0 :(得分:0)
据我了解,您只想重命名从服务器检索的数据对象的密钥。
这应该这样做:
getlist() {
this.$http.get('/getlist')
.then(res => {
let list = [];
for (let item of res.data) {
let newItem = {
category_name: item.name,
companies: item.company
}
list.push(newItem);
}
this.lists = list;
})
.catch(err => {
console.error('Error retrieving "/getlist":', err)
});
}