The loop function return can be default

        var arr = [11,22,33,44]
        //var ob = arr.some(item=>{return item>25})//正确
        var ob = arr.some(item=>{
    
    item>25})//也正确
        //检查数组是否有大于25的数
        console.log(ob)//true

After testing, it can run normally with or without return. When using arrow functions, the return of this type of loop function can be omitted.
Recently, when learning es6 writing, I found that the tutorial did not give conditional statements when using loop functions like findIndex and other return values. Add return and
note that map() returns an element, not a simple index value or boolean. It must return an element. The return cannot be followed by a judgment expression, and the array after the map has the same length as the original array. After the judgment is filtered, there is no The filtered location will return undefined

            var b=[1,2,3].map(item=>item>1?item:'')
            var c=[1,2,3].map(item=>item>1)
            console.log(b)//['',2,3]
            console.log(c)//[false,true,true],可以看出返回了判断的结果
            var arr=[1,2,3]
            var b=arr.map(i=>{
    
    if(i>1)return i})
            console.log(b)//[undefined,2,3]

Guess you like

Origin blog.csdn.net/weixin_43939111/article/details/110952054