-5

有人可以帮我构建一个循环来创建以下结构吗?值可以是任何东西。

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"
      }
  ];
4

2 回答 2

3
var foo = [];

for (var i = 0; i< 10; i++){
    foo.push({
    "adjacencies": [],
    "data": {
      "$color": "#83548B",
      "$type": "circle",
      "$dim": 10
    },
    "id": "graphnode0",
    "name": "graphnode0"
  });
}

根据需要更改值。

于 2013-03-16T22:26:50.473 回答
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
  });
}
于 2013-03-16T22:37:53.203 回答