1

My json object is

var myjson={ "elements" :[{},{},.....]};

Now I want to add some more elements with out disturbing the existing elements.

and I dont want to get use the old elements. I mean dont like this approach

var elements=myjson.elements;
elements=elements.concat(newelementslist);

Is there any other approach like

myjson.elements.append(newelementlist);

Please give me a simple solution.

kirankumar
  • 191
  • 1
  • 2
  • 13
  • 2
    `Array.prototype.push.apply(myjson.elements, newelementlist)` should work. But the real question is, what is wrong with the `.concat` approach? Edit: of course, without `apply` you would get the `newelementlist` added as a single element, not a list of elements. That makes sense... – DCoder Aug 09 '12 at 07:09
  • What's wrong with `myjson.elements.concat(newelementlist);` ? – xdazz Aug 09 '12 at 07:12
  • probably a duplicate of http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – rAjA Aug 09 '12 at 07:18
  • @xdazz In case of using concat I have to get the existing elements. It is not possible to me. I just want to append directly. – kirankumar Aug 09 '12 at 07:44
  • Where's JSON in your code? All I see is raw JavaScript objects. If it's an object, it cannot be JSON, because JSON is a *string*. – Álvaro González Aug 09 '12 at 08:37
  • @vicario look at this http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation – kirankumar Aug 09 '12 at 10:12

1 Answers1

-2

Try this

myjson.elements.push({newelement:"newelement"});
zigzag.bond
  • 308
  • 3
  • 15