js错误集锦|js错误集锦 函数在一个map循环遍历中进行返回值返回 结果在调用该函数时收不到返回值
【js错误集锦|js错误集锦 函数在一个map循环遍历中进行返回值返回 结果在调用该函数时收不到返回值】错误代码:
superviseFilter = () => {
let company = this.props.cooperativeState.allCompany
if(this.state.inspection.msName){
let list = null
company.map(item => {
if(item.comName == this.state.inspection.msName){
let a = item.comId
if(this.state.inspection.superviseType == '137-01'){//港口
let wharfs = this.props.superviseState.allWharfs
wharfs = wharfs.filter(wharf => wharf.comId == a)
return wharfs.map(wharf => wharf.wharfName)
}else if(this.state.inspection.superviseType == '137-02'){//航道
let waterways = this.props.superviseState.allWaterways
waterways= waterways.filter(waterway => waterway.orgId.includes(a))
return waterways.map(waterway => waterway.waterwayName)
}
}
})
alert('执行')
}else{
}
}
分析:在该函数的map循环遍历中进行返回,理应来说return之后他不会往下执行了,结果打印时你会发现,他继续执行了,所以说这样写是错误的
正确代码如下:
superviseFilter = () => {
let company = this.props.cooperativeState.allCompany
if(this.state.inspection.msName){
let list = null
company.map(item => {
if(item.comName == this.state.inspection.msName){
let a = item.comId
if(this.state.inspection.superviseType == '137-01'){//港口
let wharfs = this.props.superviseState.allWharfs
wharfs = wharfs.filter(wharf => wharf.comId == a)
list = wharfs.map(wharf => wharf.wharfName)
}else if(this.state.inspection.superviseType == '137-02'){//航道
let waterways = this.props.superviseState.allWaterways
waterways= waterways.filter(waterway => waterway.orgId.includes(a))
list = waterways.map(waterway => waterway.waterwayName)
}
}
})
return list
}else{
}
}
ok,我也是第一次遇到,纠结了好一会,终于解决了
推荐阅读
- 一起来学习C语言的字符串转换函数
- C语言字符函数中的isalnum()和iscntrl()你都知道吗
- C语言浮点函数中的modf和fmod详解
- C语言中的时间函数clock()和time()你都了解吗
- 唱歌教学(导致嗓音损坏的几个常见的错误唱歌方法!)
- 调取接口时报404错误(ID:16)
- 概率论/统计学|随机变量 的 分布函数 与 概率密度函数 的区别
- vue组件中为何data必须是一个函数()
- 接口|axios接口报错-参数类型错误解决
- iOS-Swift-map|iOS-Swift-map filter reduce、函数式编程