在lodash集合中使用变量名

时间:2017-06-12 05:33:54

标签: javascript node.js lodash

问题有点疯狂。是否有可能使用变量名而不是另一个,显然,请考虑以下代码,我必须切换到任何变量名称“people”或“student”相应

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

1 个答案:

答案 0 :(得分:6)

当然,只需将变量放入数组而不是字符串:

var variables=[people, student];

完整示例:

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>