结论:写了{}里面的要return,没写{}不加return
大概是写在{}里面的就算是函数了么?
应该是我ES6箭头函数没学好
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报错