2013-07-26 48 views
9

我有對象2所列出:加入在Javascript

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

是否有一個功能(可能角,JQuery的,下劃線,LoDash,或其他外部庫)做左連接在這些一行?喜歡的東西:

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

我可以寫我自己的,但如果LoDash有一個優化的版本,我想利用這一點。

回答

3

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

不,LoDash沒有加入很容易prety實現自己雖然來實現,這是不是一個相當加入,但選擇了所有的人以匹配汽車:

var peopleWithCars = _.filter(people, function (person) { 
     return _.exists(cars, function(car) { 
      return car.id === person.id; 
     }); 
    }); 
1

您可以使用JavaScript的Alasql 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]); 

試試這個例子at jsFiddle

0

這裏有一個簡單的循環,我做了一個Javascript(JQuery的在這種情況下)「加入」 OBJ1和OBJ2上someID和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; 
      } 
     }); 
    }); 
0

此示例使用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) { 
 
    \t  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'));

結果

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

0

可以在普通的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),[]);