Joins in Javascript

2019-01-22 15:52发布

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", carid: 2}];

cars=
[{id: 1, name: "Ford Fiesta", color: "blue"},
 {id: 2, name: "Ferrari", color: "red"},
 {id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}];

Is there a function (possibly in Angular, JQuery, Underscore, LoDash, or other external library) to do a left join in one line on these? Something like:

peoplewithcars = leftjoin( people, cars, "carid", "id");

I can write my own, but if LoDash has an optimised version I'd like to use that.

8条回答
霸刀☆藐视天下
2楼-- · 2019-01-22 15:58

Linq.js http://linqjs.codeplex.com/ will do joins along with many other things

查看更多
我命由我不由天
3楼-- · 2019-01-22 16:03

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");
查看更多
相关推荐>>
4楼-- · 2019-01-22 16:04

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;
        });
    });
查看更多
叛逆
5楼-- · 2019-01-22 16:11
地球回转人心会变
6楼-- · 2019-01-22 16:12

This example uses Lodash to left join the first matched object. Not quite what the question asks, but I found a similar answer helpful.

var leftTable = [{
  leftId: 4,
  name: 'Will'
}, {
  leftId: 3,
  name: 'Michael'
}, {
  leftId: 8,
  name: 'Susan'
}, {
  leftId: 2,
  name: 'Bob'
}];

var rightTable = [{
  rightId: 1,
  color: 'Blue'
}, {
  rightId: 8,
  color: 'Red'
}, {
  rightId: 2,
  color: 'Orange'
}, {
  rightId: 7,
  color: 'Red'
}];

console.clear();

function leftJoinSingle(leftTable, rightTable, leftId, rightId) {
  var joinResults = [];

  _.forEach(leftTable, function(left) {
  	      var findBy = {};
      findBy[rightId] = left[leftId];

      var right = _.find(rightTable, findBy),
          result = _.merge(left, right);

      joinResults.push(result);
  })

  return joinResults;
}


var joinedArray = leftJoinSingle(leftTable, rightTable, 'leftId', 'rightId');
console.log(JSON.stringify(joinedArray, null, '\t'));

Results

[
	{
		"leftId": 4,
		"name": "Will"
	},
	{
		"leftId": 3,
		"name": "Michael"
	},
	{
		"leftId": 8,
		"name": "Susan",
		"rightId": 8,
		"color": "Red"
	},
	{
		"leftId": 2,
		"name": "Bob",
		"rightId": 2,
		"color": "Orange"
	}
]

查看更多
做自己的国王
7楼-- · 2019-01-22 16:19

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;
            }
        });
     });
查看更多
登录 后发表回答