Joins in Javascript

后端 未结 8 2242
醉话见心
醉话见心 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

    It is not hard to implement using underscore.js

    function leftJoin(left, right, left_id, right_id) {
        var result = [];
        _.each(left, function (litem) {
            var f = _.filter(right, function (ritem) {
                return ritem[right_id] == litem[left_id];
            });
            if (f.length == 0) {
                f = [{}];
            }
            _.each(f, function (i) {
                var newObj = {};
                _.each(litem, function (v, k) {
                    newObj[k + "1"] = v;
                });
                _.each(i, function (v, k) {
                    newObj[k + "2"] = v;
                });
                result.push(newObj);
            });
        });
        return result;
    }
    
    leftJoin(people, cars, "carid", "id");
    
    0 讨论(0)
  • 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;
                }
            });
         });
    
    0 讨论(0)
  • 2020-12-14 04:19

    No, LoDash does not have join it's prety easy to implement your own though, this isn't quite a join but selects all people with a matching car:

        var peopleWithCars = _.filter(people, function (person) {
            return _.exists(cars, function(car) {
                return car.id === person.id;
            });
        });
    
    0 讨论(0)
  • 2020-12-14 04:28

    This implementation uses the ES6 spread operator. Again, not a library function as asked for.

    const leftJoin = (objArr1, objArr2, key1, key2) => objArr1.map(
        anObj1 => ({
            ...objArr2.find(
                anObj2 => anObj1[key1] === anObj2[key2]
            ),
            ...anObj1
        })
    );
    
    0 讨论(0)
  • 2020-12-14 04:28

    You can do such stuff in plain javascript.

    people.map(man => 
            cars.some(car => car.id === man.carid) ? 
                cars.filter(car => car.id === man.carid).map(car => ({car, man})) : 
                {man}
            ).reduce((a,b)=> a.concat(b),[]);
    
    0 讨论(0)
  • 2020-12-14 04:30

    You can use Alasql JavaScript SQL library to join two or more arrays of objects:

    var res = alasql('SELECT people.name AS person_name, cars.name, cars.color \
        FROM ? people LEFT JOIN ? cars ON people.carid = cars.id',[people, cars]);
    

    Try this example at jsFiddle.

    0 讨论(0)
提交回复
热议问题