0

I am generating a graph through JSON. I want to include every element of my Array in it. As you can see I am hard coding the array elements currently (relation[0], relation[1]). I want to iterate through the whole array. How ever, I obviously can't put for loop in JSON. If I try to include only array itself (relation), my graph wont generate because the whole array is wrapped with [ ] brackets. So instead of adding relation[0], relation[1], ..... I'd like that to be added automatically based on array size.

this.graph.fromJSON({
  "cells": [{
      "type": "qad.Question",
      "size": {"width": 201.8984375, "height": 125},
      "optionHeight": 30,
      "questionHeight": 45,
      "paddingBottom": 20,
      "minWidth": 150,
      "inPorts": [{"id": "in", "label": "In"}],
      "outPorts": [],
      "position": {"x": 300, "y": 38},
      "angle": 0,
      "question": objectName2,
      "options": objectCol2,
      "id": "1849d917-8a43-4d51-9e99-291799c144db",
      "z": 2
    }, relation[0],relation[1]
  ]
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dino
  • 7,779
  • 12
  • 46
  • 85
  • Duplicate: http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure – Mazz Mar 17 '17 at 09:51
  • 1
    How about you create an array with necessary config values and then concat relations into it. `var params = [{...}]; ..."cells": params.concat(relation),..` – Rajesh Mar 17 '17 at 09:53
  • @Rajesh thanks for the idea. I'll try it. – Dino Mar 17 '17 at 09:57
  • @Rajesh now the problem is when I want to add that array to this method this.graph.fromJSON(JSON.stringify(params)). The output is yet again wrapped with [ ], therefore it cant be parsed as valid JSON. ["cells": .... ] – Dino Mar 17 '17 at 10:09
  • `[cells: ...]` is not valid. It should be `{ cells: [...]}` – Rajesh Mar 17 '17 at 10:11
  • I fixed it. I was creating var array = [ { cells: [...] } ], instead of var array = { cells: [...] } – Dino Mar 17 '17 at 10:13

3 Answers3

0
for (var i = 0; i < json.length; i++) {
  var obj = json[i];

  console.log(obj.id);
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
Ashwin Golani
  • 498
  • 3
  • 10
  • 1
    Please add explanation – Rajesh Mar 17 '17 at 09:52
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Mar 17 '17 at 11:02
0

Taken from: How do I iterate over a JSON structure?

var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
    
for (var i = 0; i < arr.length; i++){
  document.write("<br><br>array index: " + i);
  var obj = arr[i];
  for (var key in obj){
    var value = obj[key];
    document.write("<br> - " + key + ": " + value);
  }
}
Community
  • 1
  • 1
Mazz
  • 1,859
  • 26
  • 38
0
var relation= [relation[0],relation[1]];
var obj = {
  "cells": [{
      "type": "qad.Question",
      "size": {"width": 201.8984375, "height": 125},
      "optionHeight": 30,
      "questionHeight": 45,
      "paddingBottom": 20,
      "minWidth": 150,
      "inPorts": [{"id": "in", "label": "In"}],
      "outPorts": [],
      "position": {"x": 300, "y": 38},
      "angle": 0,
      "question": objectName2,
      "options": objectCol2,
      "id": "1849d917-8a43-4d51-9e99-291799c144db",
      "z": 2
    }
  ]
};
this.graph.fromJSON(obj.cells.concat(relation));