关于javascript:filter箭头函数的一个bug

28次阅读

共计 412 个字符,预计需要花费 2 分钟才能阅读完成。

先上错误代码:

this.expandedRowKeys = this.expandedRowKeys.filter(item => {item !== record.index})

这个代码片段应用了花括号 {} 将箭头函数的主体包裹起来。花括号示意函数体的起始和完结,并创立了一个代码块。在这种状况下,因为短少 return 语句,箭头函数将返回 undefined,而不是条件表达式的后果。
正确的写法是在花括号 {} 中加return

this.expandedRowKeys = this.expandedRowKeys.filter(item => { return item !== record.index})

或者去掉花括号{}, 因为箭头函数会隐式返回条件表达式 item !== record.index 的后果。

this.expandedRowKeys = this.expandedRowKeys.filter(item => item !== record.index) 

正文完
 0