Lodash使用_.intersection获取id的名称

时间:2017-12-11 15:21:50

标签: javascript arrays lodash

通过使用Lodash,我希望与我的数据相交并找到每个req.user.scope的城市名称。

var citys=[{id:26,name:"CITY_A",buildings:[{id:48,name:"B1"},{id:52,name:"B2"},{id:47,name:"B3"},{id:53,name:"B4"}],menu:[{name:"LINK_A",link:"<link_to_a>"},{name:"LINK_B",link:"<link_to_b>"}]},{id:81,name:"CITY_B",buildings:[{id:106,name:"Salle B1"},{id:107,name:"B2s"},{id:108,name:"B3"},{id:109,name:"B4"},{id:110,name:"B5"}]},{id:80,name:"CITY_C",buildings:[{id:111,name:"B1"},{id:114,name:"B2"},{id:112,name:"B3"},{id:113,name:"B4"},{id:115,name:"B5"}]},{id:79,name:"CITY_D",buildings:[{id:103,name:"B1"},{id:104,name:"B2"},{id:105,name:"B3"}]}];

var req = {
    user : {
    scope : [26, 79]
  }, 
  params : {
    code : 53
  }
}

我尝试了以下内容:

console.log(_.intersectionBy(req.user.scope.map(function(id){
    return {id : id}
}), citys, "id"))

但不成功。

2 个答案:

答案 0 :(得分:0)

_.intersectionBy()方法返回第一个数组传递给它的结果,因此你应该切换传递给_.intersectionBy()的数组的顺序(城市应该是第一个):

var citys=[{id:26,name:"CITY_A",buildings:[{id:48,name:"B1"},{id:52,name:"B2"},{id:47,name:"B3"},{id:53,name:"B4"}],menu:[{name:"LINK_A",link:"<link_to_a>"},{name:"LINK_B",link:"<link_to_b>"}]},{id:81,name:"CITY_B",buildings:[{id:106,name:"Salle B1"},{id:107,name:"B2s"},{id:108,name:"B3"},{id:109,name:"B4"},{id:110,name:"B5"}]},{id:80,name:"CITY_C",buildings:[{id:111,name:"B1"},{id:114,name:"B2"},{id:112,name:"B3"},{id:113,name:"B4"},{id:115,name:"B5"}]},{id:79,name:"CITY_D",buildings:[{id:103,name:"B1"},{id:104,name:"B2"},{id:105,name:"B3"}]}];

var req = {"user":{"scope":[26,79]},"params":{"code":53}};

var result = _.intersectionBy(citys, req.user.scope.map(function(id){
    return {id : id}
}), "id");

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

您可以使用_.intersectionWith()

,而不是在相交之前进行映射

var citys = [{id:26,name:"CITY_A",buildings:[{id:48,name:"B1"},{id:52,name:"B2"},{id:47,name:"B3"},{id:53,name:"B4"}],menu:[{name:"LINK_A",link:"<link_to_a>"},{name:"LINK_B",link:"<link_to_b>"}]},{id:81,name:"CITY_B",buildings:[{id:106,name:"Salle B1"},{id:107,name:"B2s"},{id:108,name:"B3"},{id:109,name:"B4"},{id:110,name:"B5"}]},{id:80,name:"CITY_C",buildings:[{id:111,name:"B1"},{id:114,name:"B2"},{id:112,name:"B3"},{id:113,name:"B4"},{id:115,name:"B5"}]},{id:79,name:"CITY_D",buildings:[{id:103,name:"B1"},{id:104,name:"B2"},{id:105,name:"B3"}]}];

var req = {"user":{"scope":[26,79]},"params":{"code":53}};

var result = _.intersectionWith(citys, req.user.scope, function(arrVal, othVal) {
  return arrVal.id === othVal;
});

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

答案 1 :(得分:0)

我想建议一个替代方法'_.map',如果你正在寻找一种从id获取名字的方法,那么_.map听起来适合任务。

var citys = [{id:26,name:"CITY_A",buildings:[{id:48,name:"B1"},{id:52,name:"B2"},{id:47,name:"B3"},{id:53,name:"B4"}],menu:[{name:"LINK_A",link:"<link_to_a>"},{name:"LINK_B",link:"<link_to_b>"}]},{id:81,name:"CITY_B",buildings:[{id:106,name:"Salle B1"},{id:107,name:"B2s"},{id:108,name:"B3"},{id:109,name:"B4"},{id:110,name:"B5"}]},{id:80,name:"CITY_C",buildings:[{id:111,name:"B1"},{id:114,name:"B2"},{id:112,name:"B3"},{id:113,name:"B4"},{id:115,name:"B5"}]},{id:79,name:"CITY_D",buildings:[{id:103,name:"B1"},{id:104,name:"B2"},{id:105,name:"B3"}]}];

var req = {
    user : {
      scope : [26, 79, 181] // <-- add extra id, just to show unmatched case
    }, 
    params : {
      code : 53
    }
  };

var result = _.map(req.user.scope, cityId => _.find(citys, city => city.id === cityId));

console.log(result);

// to archive only 'intersect' result, you could add _.compact([...arr]) to remove undefined value from result.

var intersectedResult = _.compact(result);
console.log('intersected', intersectedResult);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>