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. 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", .... 
Sign up to request clarification or add additional context in comments.

1 Comment

Object { product_id="4", product_name="test1" is how the Chrome console prints objects.
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.

Comments

0

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

2 Comments

Why the negative vote?
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."
0

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

example:

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

1 Comment

Here's the NPM page (npmjs.org/package/merge)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.