分析代码 forEach和for in return操作

var two = function(num2,target){

        // let arr = []

        // num2.forEach((element,index) => {

        //     console.log(element)

        //     if(element + num2[index + 1]== target){

        //         console.log(index,parseInt(index+1))

        //         arr =  [index,parseInt(index+1)]

        //         // return [index,parseInt(index+1)]

        //     }

        // });

        // return arr

        for(let item in num2){

            console.log(parseInt(num2[item]) + parseInt(num2[parseInt(item + 1)]))

            if(parseInt(num2[item]) + parseInt(num2[parseInt(item + 1)])== target){

                console.log(item,parseInt(item+1))

                return [item,parseInt(item+1)]

            }

        }

    }

    console.log(two([2,7,11,15],9))

上述注释部分效果 === 下面部门

结论: for in 获取的值为字符串格式 forEach为数字格式 

           for in return之后会结束循环  forEach会继续执行


版权声明:本文为weixin_38938009原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。