2

Now I have two javascript object,

[Object { product_id="4", product_name="test1", book_final_num="9", 更多...}, Object { product_id="6", product_name="test2", book_final_num="9", 更多...}, Object { product_id="8", product_name="test3", book_final_num="7", 更多...}];

[Object { product_id="5", product_name="test", book_final_num="9", 更多...}]

I want to combine them to one like this,

[Object { product_id="4", product_name="test1", book_final_num="9", 更多...}, Object { product_id="6", product_name="test2", book_final_num="9", 更多...}, Object { product_id="8", product_name="test3", book_final_num="7", 更多...}, Object { product_id="5", product_name="test", book_final_num="9", 更多...}]
ComFreek
  • 28,220
  • 17
  • 99
  • 151
user2228647
  • 69
  • 2
  • 8

4 Answers4

3
var result = array1.concat(array2);

BTW, there is something strange in your object notation. It should be

[{ product_id:"4", product_name:"test1" ...

instead of

[Object { product_id="4", product_name="test1", ....
Benoit Blanchon
  • 11,973
  • 4
  • 63
  • 74
2

Are you looking for.extends like this:-

var c = $.extend({}, a, b);

or may be like this

var array3 = array1.concat(array2);

Also check out the related Thread.

Community
  • 1
  • 1
Rahul Tripathi
  • 161,154
  • 30
  • 262
  • 319
0

JQuery does it http://api.jquery.com/jQuery.merge. All done and dusted.

Ed Heal
  • 57,599
  • 16
  • 82
  • 120
  • 1
    Why the negative vote? – Ed Heal Oct 13 '13 at 09:14
  • Didn't come from me, but probably because you gave a jQuery solution when the question wasn't tagged as such. From the JavaScript category description: "Unless a tag for a framework/library is also included, a pure JavaScript answer is expected." – Scott Oct 18 '13 at 09:20
0

If you're using NodeJS, check out Merge (https://github.com/yeikos/js.merge)

example:

merge({ one: 'hello' }, { two: 'world' })
astone26
  • 1,192
  • 10
  • 16