1

I have this JSON array

{"nodes":[
        {"type":"simple-node","left":500,"id":"node-start","content":"Start"},
        {"type":"simple-node","left":500,"id":"node-1","content":"Ironing"},
        {"type":"simple-node","left":500,"id":"node-end""content":"End"}
         ],

"connections":[
        {"start":"node-start","end":"node-1"},
        {"start":"node-4","end":"node-5"}
              ]
}

I need to dynamically add "top" attribute to each element in nodes array so that it may look like this

{"nodes":[
        {"type":"simple-node","left":500,"top":3403.252685546875,"id":"node-start","content":"Start"},
        {"type":"simple-node","left":500,"top":3703.252685546875,"id":"node-1","content":"Ironing"},
        {"type":"simple-node","left":500,"top":3903.252685546875,"id":"node-end""content":"End"}
         ],

"connections":[
        {"start":"node-start","end":"node-1"},
        {"start":"node-4","end":"node-5"}
              ]
3
  • 3
    iterate and apply. Commented May 18, 2016 at 17:01
  • Is the JSON parsed? Where are those new top numbers coming from? Are they random numbers? What have you tried? There's not enough info here to give a decent answer. Commented May 18, 2016 at 17:08
  • @Squint yes json is parsed. new top is coming from the offset of a control Commented May 18, 2016 at 17:50

2 Answers 2

1

You can use forEach on obj.nodes and add object property

var obj = {"nodes":[{"type":"simple-node","left":500,"id":"node-start","content":"Start"},{"type":"simple-node","left":500,"id":"node-1","content":"Ironing"},{"type":"simple-node","left":500,"id":"node-end","content":"End"}],"connections":[{"start":"node-start","end":"node-1"},{"start":"node-4","end":"node-5"}]}

obj.nodes.forEach((e) => {
  e.top = 3403.252685546875;
});

console.log(obj)

Update: First you need to turn your json string to object with JSON.parse(yourjson) if you didn't.

Sign up to request clarification or add additional context in comments.

2 Comments

This will work if the JSON is already parsed and if the OP wanted to add the same number to each object. The question doesn't indicate either of those.
var obj = JSON.parse($("#hdnchartJSON").val()); var i = 100; obj.nodes.forEach((e) => { e.top = $("#Label2").offset().top + i; i = i + 100; }); Solved
0
    var obj={"nodes":[
       {"type":"simple-node","left":500,"id":"node-start","content":"Start"},
       {"type":"simple-node","left":500,"id":"node-1","content":"Ironing"},
       {"type":"simple-node","left":500,"id":"node-end""content":"End"}
     ]};
      var top_array=[2495,4985,3467];
      for(i=0;i<obj.nodes.length;i++){
          obj.nodes[i].top=top_array[i];
       }

In case you need to copy the keys from an array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.