Joins in Javascript

后端 未结 8 2268
醉话见心
醉话见心 2020-12-14 03:31

I have 2 lists of objects:

people = 
[{id: 1, name: \"Tom\", carid: 1},
 {id: 2, name: \"Bob\", carid: 1},
 {id: 3, name: \"Sir Benjamin Rogan-Josh IV\", car         


        
8条回答
  •  粉色の甜心
    2020-12-14 04:16

    Here's a simple loop I did for a Javascript (JQuery in this case) to "join" obj1 and obj2 on someID and add one property from obj2 to obj1.

    If you want to do a more complete join, you can go through and expand it to loop on obj2.hasOwnProperty() and copy that over as well.

        $.each(obj1,function(i){
            $.each(obj2, function(k){
                if (obj2[k].someID == obj1[i].someID ){
                    obj1[i].someValue = obj2[k].someValue;
                }
            });
         });
    

提交回复
热议问题