[英]Is there any way to break out of a function by running another function in JS
[英]JS Break out of function
我有一个看起来像这样的方法:
return AddedSoftware (software) {
this.softwares.map(function(soft) {
if(soft.id == software) {
return software.name;
}
})
}
因此,当soft.id == software
时,如何中断并返回,现在它在返回之前遍历整个softwares
!
您可以使用find()
代替
return function AddedSoftware (software) {
let res = this.softwares.find(soft => soft.id == software);
// return the software's name if there's a match, or undefined
return res ? res.name : res;
}
这将为您提供第一个符合您条件的对象。 然后,您可以从该对象获取software.name
。
摘录自文档:
find()方法返回满足提供的测试功能的数组中第一个元素的值。 否则返回undefined。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.