javascript - wp_enqueue_script - wp_register_script




加入Javascript (5)

Linq.js http://linqjs.codeplex.com/将与其他许多事物一起进行连接

我有2个对象列表:

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"}];

是否有一个函数(可能在Angular,JQuery,Underscore,LoDash或其他外部库)在这些行上进行左连接? 就像是:

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

我可以写我自己的,但如果LoDash有一个优化版本,我想使用它。


使用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");

您可以使用Alasql JavaScript SQL库来连接两个或更多个对象数组:

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]);

尝试在jsFiddle这个例子。


这里有一个简单的循环,我为Javascript(在这种情况下是JQuery)做了一个简单的循环,用于在someID上“连接”obj1和obj2,并将一个属性从obj2添加到obj1。

如果你想做一个更完整的连接,你可以通过并展开它来在obj2.hasOwnProperty()上循环,并复制它。

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

这个例子使用Lodash来加入第一个匹配的对象 。 不太完全是这个问题,但我发现一个类似的答案有帮助。

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'));

结果

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





lodash