0

I have JSON objects being created dynamically as:

[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}] [{"fill":"none","stroke":"#000000","path":"M73.5,42L73.5,42L75.5,43L82.5,46L101.5,55L119.5,65L126.5,69L128.5,71L129.5,71","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}] ....... 

I want to append all these Objects being generated into a single Javascript object as :

[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}, {"fill":"none","stroke":"#000000","path":"M73.5,42L73.5,42L75.5,43L82.5,46L101.5,55L119.5,65L126.5,69L128.5,71L129.5,71","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}] 

In this way every object being created should be appended to this JSON string. I am able to concatenate two JSON objects and have it in a different Javascript variable as:

var obj1 = '[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]'; var obj2 = '[{"fill":"none","stroke":"#000000","path":"M186.5,25L187.5,25L187.5,26L188.5,27L189.5,28L189.5,29L190.5,29","stroke-opacity":1,"stroke-width":5,"stroke-linecap":"round","stroke-linejoin":"round","transform":[],"type":"path"}]'; var mergedJS = JSON.parse(obj1).concat(JSON.parse(obj2)); mergedJSON =JSON.stringify(mergedJS); 

I however, want all the newly generated JSON objs in the same variable. Could anyone please let me know how can I do this?

9
  • How are these objects generated? Can't you push them on an array directly?
    – phylax
    CommentedApr 19, 2014 at 15:25
  • I cannot push them directly to a array, because they are generated individually out of a function call. If possible how can I push them into an array directly?CommentedApr 19, 2014 at 15:26
  • But this function gets called, and does it return the objects? Where do theses objects live?
    – phylax
    CommentedApr 19, 2014 at 15:27
  • Use var mergedJS = JSON.parse(obj1).concat(JSON.parse(obj2));
    – drkunibar
    CommentedApr 19, 2014 at 15:28
  • Yes, these objects are created as $("#data2").val(sketchpad.json()); and are assigned to a hidden div with ID = "data2"CommentedApr 19, 2014 at 15:29

1 Answer 1

1

You'll need to pull the objects out of their individual arrays before adding them to the master array:

var newJSArray = []; var mergedJS = JSON.parse(obj1); newJSArray.push(mergedJS[0]); mergedJS = JSON.parse(obj2); newJSArray.push(mergedJS[0]); 

Obviously for n objects you'll be looping that instead of as I've done above.

2
  • I want every object being created being added to the array at that moment.CommentedApr 19, 2014 at 15:57
  • This code will accomplish the same thing. Just grab the 0-th element of each object array since your objects are being sent in an enclosing array.CommentedApr 19, 2014 at 16:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.