最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Concatenate 2 arrays with object in javascriptjquery - Stack Overflow

matteradmin3PV0评论

I have the following array

var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}]

And i am trying to add the following array into it

var y = [{"id":"75769d11","title":"newtitle"}]

What i am trying to do is to merge somehow the 2 arrays into 1. The final array should be

[{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18},{"id":"75769d11","title":"newtitle"}]

Have tried

$.merge(x,y) 
// x.join(y) 
// x.push(y)   

javascript

x.concat(y)

Any idea would be useful. Regards

I have the following array

var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}]

And i am trying to add the following array into it

var y = [{"id":"75769d11","title":"newtitle"}]

What i am trying to do is to merge somehow the 2 arrays into 1. The final array should be

[{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18},{"id":"75769d11","title":"newtitle"}]

Have tried

$.merge(x,y) 
// x.join(y) 
// x.push(y)   

javascript

x.concat(y)

Any idea would be useful. Regards

Share Improve this question edited Dec 17, 2018 at 14:11 Jai 74.7k12 gold badges76 silver badges104 bronze badges asked Dec 17, 2018 at 14:01 Mike XMike X 1291 silver badge7 bronze badges 3
  • 3 Use x.push(y[0]) – Mohammad Commented Dec 17, 2018 at 14:04
  • 3 x.concat(y) worked for me. What is the issue? – Jai Commented Dec 17, 2018 at 14:06
  • These arrays are strings , so i think that is why i can't concat them or push, as it seems it gives error. Think i need to transform from string into array ? – Mike X Commented Dec 17, 2018 at 14:08
Add a ment  | 

5 Answers 5

Reset to default 3

Well, Array.concat() method returns a new merged array. So, you have to store it in a variable to log it:

var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}]

var y = [{"id":"75769d11","title":"newtitle"}]

var merged = x.concat(y);

console.log(merged);

using jQuery

 var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}]
 var y = [{"id":"75769d11","title":"newtitle"}];
 var mergedArray=$.merge(x,y);
 var mergedString=JSON.stringify(mergedArray);
 console.log(mergedString);

var newArray=x.concat(y) will work.

With ES6, You can also use spread operator .... Spread operator turns the items of an iterable(arrays, array like objects) into arguments of a function call or into elements of an Array. So, to get new merged array you can do

var newArray=[...x, ...y]

If you want to merge the elements of x and y into x, you can still use spread operator as x.push(...y)

var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}]

var y = [{"id":"75769d11","title":"newtitle"}]

var merged = x.concat(y);
var mergedArray = [...x, ...y];
Post a comment

comment list (0)

  1. No comments so far