admin管理员组

文章数量:1023018

I have two JSON feeds. One contains the basic information about a course, the 2nd contains information that is of a more administrative nature. Here is a sample of what I mean.

FIRST

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36"}}
   ,{"course":{"id":"5","title":"Job hunting online","body":"29"}}
}

SECOND

{"courses":
   {"4": {"id":4,"name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}
   ,"5": {"id":5,"name":"Online Basics","title":"OB2 Job hunting online","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/11\/24 02:14:51","on_planner":false}
   }
}

DESIRED OUTPUT

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36","name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}}
}

I hope the addition of a "desired output" option will make it easier to understand. Even though I have only put 1 example into the desired output area, I want all records where the id's match to merge.

Any suggestions?

Thanks.

I have two JSON feeds. One contains the basic information about a course, the 2nd contains information that is of a more administrative nature. Here is a sample of what I mean.

FIRST

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36"}}
   ,{"course":{"id":"5","title":"Job hunting online","body":"29"}}
}

SECOND

{"courses":
   {"4": {"id":4,"name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}
   ,"5": {"id":5,"name":"Online Basics","title":"OB2 Job hunting online","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/11\/24 02:14:51","on_planner":false}
   }
}

DESIRED OUTPUT

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36","name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}}
}

I hope the addition of a "desired output" option will make it easier to understand. Even though I have only put 1 example into the desired output area, I want all records where the id's match to merge.

Any suggestions?

Thanks.

Share Improve this question edited Feb 26, 2013 at 15:02 Phil James asked Feb 26, 2013 at 14:26 Phil JamesPhil James 1132 silver badges10 bronze badges 7
  • What have you tried? - First post - Second post - Third post If neither of these are helpful, why? What makes your problem different from others? – Michal Commented Feb 26, 2013 at 14:31
  • First post is just about merging two identical feeds together... – Phil James Commented Feb 26, 2013 at 14:36
  • Second post I don't know what its about as the data samples cannot be accessed anymore – Phil James Commented Feb 26, 2013 at 14:37
  • Third post again is about merging two objects into one... – Phil James Commented Feb 26, 2013 at 14:38
  • What I want to do is to bine the JSON elements into one element if their id numbers match. I DO NOT want to just add the two JSON feeds into one long feed... And by JSON element, I mean one record. – Phil James Commented Feb 26, 2013 at 14:39
 |  Show 2 more ments

1 Answer 1

Reset to default 7
$.when($.get("feed1.json"), $.get("feed2.json")).done(function(basics, admin) {
    var basiccourses = basics[0].courses,
        admincourses = admins[0].courses;
    // merge all basic course objects into the admin courses object
    for (var i=0; i<basiccourses.length; i++) {
        var basic = basiccourses[i];
        if (basic.id in admincourses)
            // by extending the course object
            $.extend(admincourses[basic.id], basic);
        else
            // or just copying it over
            admincourses[basic.id] = basic;
    }

    // now admincourses has all information bined
});

I have two JSON feeds. One contains the basic information about a course, the 2nd contains information that is of a more administrative nature. Here is a sample of what I mean.

FIRST

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36"}}
   ,{"course":{"id":"5","title":"Job hunting online","body":"29"}}
}

SECOND

{"courses":
   {"4": {"id":4,"name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}
   ,"5": {"id":5,"name":"Online Basics","title":"OB2 Job hunting online","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/11\/24 02:14:51","on_planner":false}
   }
}

DESIRED OUTPUT

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36","name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}}
}

I hope the addition of a "desired output" option will make it easier to understand. Even though I have only put 1 example into the desired output area, I want all records where the id's match to merge.

Any suggestions?

Thanks.

I have two JSON feeds. One contains the basic information about a course, the 2nd contains information that is of a more administrative nature. Here is a sample of what I mean.

FIRST

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36"}}
   ,{"course":{"id":"5","title":"Job hunting online","body":"29"}}
}

SECOND

{"courses":
   {"4": {"id":4,"name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}
   ,"5": {"id":5,"name":"Online Basics","title":"OB2 Job hunting online","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/11\/24 02:14:51","on_planner":false}
   }
}

DESIRED OUTPUT

{"courses":
    {"course":{"id":"4","title":"Using a puter","body":"36","name":"Online Basics","title":"Using a puter","shortname":"onlinebasics","height":640,"width":980,"html5":1,"url":"\/sites\/default\/files\/courses\/onlinebasics\/wrapper.html","started_on":"","bundle_only":true,"progress":false,"pleted_on":"2012\/10\/31 00:12:39","on_planner":true}}
}

I hope the addition of a "desired output" option will make it easier to understand. Even though I have only put 1 example into the desired output area, I want all records where the id's match to merge.

Any suggestions?

Thanks.

Share Improve this question edited Feb 26, 2013 at 15:02 Phil James asked Feb 26, 2013 at 14:26 Phil JamesPhil James 1132 silver badges10 bronze badges 7
  • What have you tried? - First post - Second post - Third post If neither of these are helpful, why? What makes your problem different from others? – Michal Commented Feb 26, 2013 at 14:31
  • First post is just about merging two identical feeds together... – Phil James Commented Feb 26, 2013 at 14:36
  • Second post I don't know what its about as the data samples cannot be accessed anymore – Phil James Commented Feb 26, 2013 at 14:37
  • Third post again is about merging two objects into one... – Phil James Commented Feb 26, 2013 at 14:38
  • What I want to do is to bine the JSON elements into one element if their id numbers match. I DO NOT want to just add the two JSON feeds into one long feed... And by JSON element, I mean one record. – Phil James Commented Feb 26, 2013 at 14:39
 |  Show 2 more ments

1 Answer 1

Reset to default 7
$.when($.get("feed1.json"), $.get("feed2.json")).done(function(basics, admin) {
    var basiccourses = basics[0].courses,
        admincourses = admins[0].courses;
    // merge all basic course objects into the admin courses object
    for (var i=0; i<basiccourses.length; i++) {
        var basic = basiccourses[i];
        if (basic.id in admincourses)
            // by extending the course object
            $.extend(admincourses[basic.id], basic);
        else
            // or just copying it over
            admincourses[basic.id] = basic;
    }

    // now admincourses has all information bined
});

本文标签: javascriptMerge two JSON objects that have a common elementStack Overflow