【vue】在map循环中return返回值一直返回空
btnClick(eleRef) {let result = this.test();this.$alert(result);},test(){let flag = false;let arr=[{title:0},{title:1},{title:2},{title:3},{title:4}];arr.map(one => {if(one.title == 2){fla.
·
btnClick(eleRef) {
let result = this.test();
this.$alert(result);
},
test(){
let flag = false;
let arr=[{title:0},{title:1},{title:2},{title:3},{title:4}];
arr.map(one => {
if(one.title == 2){
flag = true;
console.log('one.title == 2');
return one;
}
})
if(!flag){
return -1;
}
},
以上代码运行结果,一直返回空。
以下代码,则能正常返回期望结果
btnClick(eleRef) {
let result = this.test();
this.$alert(result);
},
test(){
let arr=[{title:0},{title:1},{title:2},{title:3},{title:4}];
let result = -1;
arr.map(one => {
if(one.title == 2){
result = one.title;
}
})
return result;
},
返回结果:
更多推荐
已为社区贡献10条内容
所有评论(0)