简体   繁体   中英

Lodash get name of id using _.intersection

By using Lodash, i want to intersect my data and find city name of each 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
  }
}

i tried the following :

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

But not successfully.

The _.intersectionBy() method returns the results from the 1st array pass to it, so you should switch the order of the arrays passed to _.intersectionBy() (cities should be first):

 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> 

Instead of mapping before intersecting, you can use _.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> 

I would like to suggest an alternative, '_.map', if you're looking for a way to get name from id, then _.map sounds fit for the task.

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM