有人可以帮我构建一个可以创建以下结构的循环吗?价值观可以是任何东西。
var json = [
{
"adjacencies": [],
"data": {
"$color": "#83548B",
"$type": "circle",
"$dim": 10
},
"id": "graphnode0",
"name": "graphnode0"
}, {
"adjacencies": [],
"data": {
"$color": "#EBB056",
"$type": "triangle",
"$dim": 9
},
"id": "graphnode18",
"name": "graphnode18"
}, {
"adjacencies": [],
"data": {
"$color": "#70A35E",
"$type": "circle",
"$dim": 8
},
"id": "graphnode19",
"name": "graphnode19"
}, {
"adjacencies": [],
"data": {
"$color": "#C74243",
"$type": "star",
"$dim": 8
},
"id": "graphnode20",
"name": "graphnode20"
}
];
答案 0 :(得分:3)
var foo = [];
for (var i = 0; i< 10; i++){
foo.push({
"adjacencies": [],
"data": {
"$color": "#83548B",
"$type": "circle",
"$dim": 10
},
"id": "graphnode0",
"name": "graphnode0"
});
}
根据需要更改值。
答案 1 :(得分:0)
您可以使用此功能,但我不知道您在哪里获得颜色,类型和暗淡值:
var data = [] ;
var total = 6 ;
for (var i=0 ; i<total ; i++){
data.push({
"adjacencies": [],
"data": {
"$color": "the-color",
"$type": "the-type",
"$dim": 10 // the dim
},
"id": "graphnode"+i,
"name": "graphnode"+i
});
}