结论:写了{}里面的要return,没写{}不加return
let arr = [
{
id: 1,
name: 'aa',
desc: 'aaaa'
},
{
id: 2,
name: 'bb'
},
{
id: 3,
name: 'aa'
}
]
let arr1 = arr.filter(item => item.name == 'aa')//正确
// let arr2 = arr.filter(item => return item.name == 'aa' ) //报错
let arr3 = arr.filter(item => {item.name == 'aa'})//为空
let arr4 = arr.filter(item => { return item.name == 'aa' })//正确
console.log(arr1, 'arr1')
// console.log(arr2, 'arr2');
console.log(arr3, 'arr3');
console.log(arr4, 'arr4');
arr1,arr4能正确输出,arr3没有取到正确的值是空的,arr2报错
原文转载于https://www.cnblogs.com/zcbj588/p/14699411.html