0

I have two javascript object with data like 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 you can see, the properties etc vary a bit, and of course the values as well. Also they don't have the same number of items in them. I'm trying to merge them like this:

$.extend(true,posts, posts_object);

(posts and posts_objects contain the two objects)

I want to merge them into 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, this results in the data from the second object to overwrite the data of the first object. Is there any way to merge them but instead add the items from the second object to the first object?

4
  • 3
    Yes, there is, but you'll have to write your own code to do it. You'll have to decide what's supposed to happen when both objects have a "foo" property, for example. Commented Jun 6, 2012 at 12:08
  • 1
    @MarkSchultheiss I think that's supposed to be just a sample of what the objects look like. Commented Jun 6, 2012 at 12:12
  • Perhaps, but I don't know from this if the OP just wants to use .push() to extend the data: array or something else like add a second data property and make it an array of those. Difficult to say for sure. Commented Jun 6, 2012 at 12:16
  • I made some clarifications... Commented Jun 6, 2012 at 12:34

1 Answer 1

1

If by "add the items", you mean that you want all properties that are in the second object but not in the first to end up being added to the first one, then:

var tmp;
$.extend(true, tmp, posts);
$.extend(true, posts, posts_object);
$.extend(true, posts, tmp);

might work. It all depends on what copying semantics you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.