我需要制作以下结构:
"rows":[
{//panel1
},
{//panel2
}
{//panel3 ...
}
]
所以我创建了方法extend,所以我试图这样做,但它不起作用,因为它只返回我最后一个JSON:
var panel_cpu = {"title": "iNeedHelp","smt": 5}
var panel_swap = {"title": "iNeedHelp2","smt": 6}
var combined = $.extend({}, panel_cpu, panel_swap);
//following the comments...
var arr = [];
arr.push(panel_cpu);
arr.push(panel_swap);
////////////////////////////
//following 1 answer
var combined2 = $.extend({}, [panel_cpu, panel_swap]);
//////////////////////////////////////////////////////
var big_panel = {"rows": arr}
console.log(big_panel);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
这里使用的是JQuery,因为我没有找到下划线库
答案 0 :(得分:0)
您可以按如下方式实现输出:
var panel_cpu = [{
"title": "iNeedHelp",
"smt": 5
}]
var panel_swap = [{
"title": "iNeedHelp2",
"smt": 6
}]
var combined = panel_cpu.concat(panel_swap);
var big_panel = [{
"rows": [combined]
}]
console.log(big_panel);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
但是,您需要拥有panel_cpu
和panel_swqp
变量的数组值。我会发布一个简单的答案。
修改1
更改此选项将获得如下输出,
var big_panel = {
"rows": combined
}
var panel_cpu = [{
"title": "iNeedHelp",
"smt": 5
}]
var panel_swap = [{
"title": "iNeedHelp2",
"smt": 6
}]
var combined = panel_cpu.concat(panel_swap);
var big_panel = {
"rows": combined
}
console.log(big_panel);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>