user2924127 user2924127 - 7 months ago 13
Javascript Question

Merge these 2 arrays

I have an array I start out with:

var array1 = [{"key":{"id":1,"kind":"Table","path":["Table",1]},"data":{"txt":"some txt","modified":1461294950,"cat1":"a1","permission":"n"}},
{"key":{"id":2,"kind":"Table","path":["Table",2]},"data":{"txt":"some txt","modified":1461294953,"cat1":"a1","permission":"n"}},
{"key":{"id":3,"kind":"Table","path":["Table",3]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":4,"kind":"Table","path":["Table",4]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":5,"kind":"Table","path":["Table",5]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}}];


This array has 5 elements. I then from my server get 5 more results. I would like to append this new array to the existing.

So I receive this from my server:

var server_response = [{"key":{"id":6,"kind":"Table","path":["Table",6]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":7,"kind":"Table","path":["Table",7]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":8,"kind":"Table","path":["Table",8]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":9,"kind":"Table","path":["Table",9]},"data":{"txt":"some txt","modified":1461294955,"cat1":"a1","permission":"n"}},
{"key":{"id":10,"kind":"Table","path":["Table",10]},"data":{"txt":"some txt","modified":1461294955,"cat1":"a1","permission":"n"}}];


So to merge them I go:

console.log(array1.push(server_response);


But I get the following result:


[{"key":{"id":1,"kind":"Table","path":["Table",1]},"data":{"txt":"some
txt","modified":1461294950,"cat1":"a1","permission":"n"}},{"key":{"id":2,"kind":"Table","path":["Table",2]},"data":{"txt":"some
txt","modified":1461294953,"cat1":"a1","permission":"n"}},{"key":{"id":3,"kind":"Table","path":["Table",3]},"data":{"txt":"some
txt","modified":1461294954,"cat1":"a1","permission":"n"}},{"key":{"id":4,"kind":"Table","path":["Table",4]},"data":{"txt":"some
txt","modified":1461294954,"cat1":"a1","permission":"n"}},{"key":{"id":5,"kind":"Table","path":["Table",5]},"data":{"txt":"some
txt","modified":1461294954,"cat1":"a1","permission":"n"}},[{"key":{"id":6,"kind":"Table","path":["Table",6]},"data":{"txt":"some
txt","modified":1461294954,"cat1":"a1","permission":"n"}},{"key":{"id":7,"kind":"Table","path":["Table",7]},"data":{"txt":"some
txt","modified":1461294954,"cat1":"a1","permission":"n"}},{"key":{"id":8,"kind":"Table","path":["Table",8]},"data":{"txt":"some
txt","modified":1461294954,"cat1":"a1","permission":"n"}},{"key":{"id":9,"kind":"Table","path":["Table",9]},"data":{"txt":"some
txt","modified":1461294955,"cat1":"a1","permission":"n"}},{"key":{"id":10,"kind":"Table","path":["Table",10]},"data":{"txt":"some
txt","modified":1461294955,"cat1":"a1","permission":"n"}}]
]


As you can see it adds the second array as an actual array to the first array. I just want the content of the server response to be added (essentially so there should be no square brackets). I know I could do this using regex, but this does not seem like a good solution. How can I merge these arrays?

Answer

I believe you are looking for concat

var array1 = [{"key":{"id":1,"kind":"Table","path":["Table",1]},"data":{"txt":"some txt","modified":1461294950,"cat1":"a1","permission":"n"}},
{"key":{"id":2,"kind":"Table","path":["Table",2]},"data":{"txt":"some txt","modified":1461294953,"cat1":"a1","permission":"n"}},
{"key":{"id":3,"kind":"Table","path":["Table",3]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":4,"kind":"Table","path":["Table",4]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":5,"kind":"Table","path":["Table",5]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}}];
var server_response = [{"key":{"id":6,"kind":"Table","path":["Table",6]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":7,"kind":"Table","path":["Table",7]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":8,"kind":"Table","path":["Table",8]},"data":{"txt":"some txt","modified":1461294954,"cat1":"a1","permission":"n"}},
{"key":{"id":9,"kind":"Table","path":["Table",9]},"data":{"txt":"some txt","modified":1461294955,"cat1":"a1","permission":"n"}},
{"key":{"id":10,"kind":"Table","path":["Table",10]},"data":{"txt":"some txt","modified":1461294955,"cat1":"a1","permission":"n"}}];
console.log(array1.concat(server_response));
Comments