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", 更多...}]
1
  • 3
    Those are not just any 2 objects - they look like Arrays to me. It's important to specify.
    – Nathan H
    Commented Oct 13, 2013 at 8:41

4 Answers 4

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", ....
1
  • Object { product_id="4", product_name="test1" is how the Chrome console prints objects. Commented Oct 13, 2013 at 8:49
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.

0

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

2
  • 1
    Why the negative vote?
    – Ed Heal
    Commented Oct 13, 2013 at 9: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
    Commented Oct 18, 2013 at 9:20
0

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

example:

merge({ one: 'hello' }, { two: 'world' })
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.