Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate 2 arrays with object in javascript / jquery

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

like image 460
Mike X Avatar asked Apr 09 '26 18:04

Mike X


1 Answers

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);
like image 159
Jai Avatar answered Apr 11 '26 07:04

Jai