我有一个看起来像这样的方法:
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
。
文档摘录:
The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
关于javascript - JS 跳出函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40450016/