![](/img/trans.png)
[英]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.