Merge two javascript objects without overwriting -
i have 2 javascript object data this:
{ "data": [ { "foo": "2388163605_10150954953808606", "bar": { "xyz": "123", }, "name": { "name": [ { "content": 1, "data": "some text", "id": "2388163605" } ] }, }, { "not_the_same": "other values", "foo": "different", "bar": { "xyz": "123", }, "name": { "name": [ { "content": 1, "data": "some text", "id": "2388163605" } ] }, }] }
second one:
{ "data": [ { "foo": "123+09", "bar": { "xyz": "1adad0", }, }, }] }
as can see, properties etc vary bit, , of course values well. don't have same number of items in them. i'm trying merge them this:
$.extend(true,posts, posts_object);
(posts , posts_objects contain 2 objects)
i want merge them this:
{ "data": [ { "foo": "2388163605_10150954953808606", "bar": { "xyz": "123", }, "name": { "name": [ { "content": 1, "data": "some text", "id": "2388163605" } ] }, }, { "not_the_same": "other values", "foo": "different", "bar": { "xyz": "123", }, "name": { "name": [ { "content": 1, "data": "some text", "id": "2388163605" } ] }, }, { "foo": "123+09", "bar": { "xyz": "1adad0", }, }, }] }
however, results in data second object overwrite data of first object. there way merge them instead add items second object first object?
if "add items", mean want properties are in second object not in first end being added first one, then:
var tmp; $.extend(true, tmp, posts); $.extend(true, posts, posts_object); $.extend(true, posts, tmp);
might work. depends on copying semantics want.
Comments
Post a Comment