gpt4 book ai didi

javascript - 在 lodash 集合中使用变量名

转载 作者:数据小太阳 更新时间:2023-10-29 05:17:42 24 4
gpt4 key购买 nike

问题有点疯狂。是否有可能使用变量名而不是另一个,显然,请考虑以下代码,我必须相应地切换到任何变量名“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);

最佳答案

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

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>

关于javascript - 在 lodash 集合中使用变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44492015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com