2012-08-09 51 views
1

我的JSON對象是如何直接附加Json對象?

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

現在我想添加一些多種元素,出來擾亂現有元素。

我不想使用舊的元素。 我的意思是不喜歡這種做法

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

有沒有像

myjson.elements.append(newelementlist); 

請給我一個簡單的解決方案的任何其他方法。

+2

'Array.prototype.push.apply(myjson.elements,newelementlist)'應該工作。但真正的問題是,'.concat'方法有什麼問題?編輯:當然,如果沒有「應用」,您會將'newelementlist'添加爲單個元素,而不是元素列表。這很有道理...... – DCoder 2012-08-09 07:09:47

+0

'myjson.elements.concat(newelementlist);'?有什麼問題? – xdazz 2012-08-09 07:12:56

+0

可能是重複的http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – rAjA 2012-08-09 07:18:26

回答

-2

試試這個

myjson.elements.push({newelement:"newelement"}); 
+0

你讀過這個問題。 – kirankumar 2012-08-09 10:13:22