简体   繁体   中英

using variable name in lodash collection

Question is bit crazy. Is there any possibility to using variable name instead of another, clearly, consider the following code where I have to switch over to any variable name "people" or "student" accordingly

 var people=[
      {name:"akash",age:25},
      {name:"abi",age:22}
   ];

 var student =[
     {name:"Sanjai",age:25},
     {name:"Ravi",age:35},
      {name:"Bopara",age:36}
 ];
var variables=["people","student"];

 var result= _.find(variables[0], function(o) { return o.age < 35; });
   console.log(result);  

Sure, just put variables into array instead of strings:

var variables=[people, student];

Full example:

 var people=[ {name:"akash",age:25}, {name:"abi",age:22} ]; var student =[ {name:"Sanjai",age:25}, {name:"Ravi",age:35}, {name:"Bopara",age:36} ]; var variables=[people, student]; var result= _.find(variables[0], function(o) { return o.age < 35; }); console.log(result);
 <script src="https://cdn.jsdelivr.net/lodash/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