forEach的问题

forEach的问题1

        var nums = [1,2,3,4,5,6];
            var obj = {
                name : 'iceman'
            }
            var name = 'shoushou';
            function foo (item , index){
                console.log(index + ' , ' + item + ' , ' + this.name);
            }
            nums.forEach(foo, this)
        /* 在node.js中运行的结果
        0 , 1 , undefined
        1 , 2 , undefined
        2 , 3 , undefined
        3 , 4 , undefined
        4 , 5 , undefined
        5 , 6 , undefined
        */

        /* 在浏览器中运行的结果
         0 , 1 , shoushou
         1 , 2 , shoushou
         2 , 3 , shoushou
         3 , 4 , shoushou
         4 , 5 , shoushou
         5 , 6 , shoushou
        */

forEach的问题2

let list = [
            {
                process: '进行中'
            },
            {
                process: '进行中'
            },
            {
                process: '进行中'
            },
        ]
        let obj = {       
            process: '完成'     
            }     
        list.forEach((item, index) => {       
            item = obj     
            console.log('item==',item)
            console.log('每一项==',list[index])
        })
        console.log('最终数据:',list)

        /* 
        运行结果
        item== {process: "完成"}
        每一项== {process: "进行中"}

        demo.html:182 item== {process: "完成"}
        每一项== {process: "进行中"}

        demo.html:182 item== {process: "完成"}
        每一项== {process: "进行中"}

        //最终的数据
        0: {process: "进行中"}
        1: {process: "进行中"}
        2: {process: "进行中"}
        */

使用forEach的解决办法,改变对象下对应的值

let list = [
            {
                process: '进行中'
            },
            {
                process: '进行中'
            },
            {
                process: '进行中'
            },
        ]
        let obj = {       
            process: '完成'     
            }     
        list.forEach((item, index) => {       
            item.process = obj.process   
            console.log('item==',item)
            console.log('每一项==',list[index])
        })
        console.log('最终数据:',list)

        /* 
        运行结果
        item== {process: "完成"}
        每一项== {process: "完成"}

        demo.html:182 item== {process: "完成"}
        每一项== {process: "完成"}

        demo.html:182 item== {process: "完成"}
        每一项== {process: "完成"}
        //最终的数据
        0: {process: "完成"}
        1: {process: "完成"}
        2: {process: "完成"}
        */

注意:
map也会出现同样的情况
使用map的解决办法是,每次遍历返回出去即可

let list = [
            {
                process: '进行中'
            },
            {
                process: '进行中'
            },
            {
                process: '进行中'
            },
        ]
        let obj = {       
            process: '完成'     
            }     
        list = list.map((item, index) => {       
            item = obj 
            console.log('item==',item)
            console.log('每一项==',list[index])
            return item // 使用map返回就可以解决这个问题
        })
        console.log('最终数据:',list)

        /* 
        运行结果
        item== {process: "完成"}
        每一项== {process: "进行中"}

        demo.html:188 item== {process: "完成"}
        每一项== {process: "进行中"}

        demo.html:188 item== {process: "完成"}
        每一项== {process: "进行中"}

        //最终的数据
        0: {process: "完成"}
        1: {process: "完成"}
        2: {process: "完成"}
        */

针对情况2,也可以直接使用for来解决

let list = [
    {
        process: '进行中'
    },
    {
        process: '进行中'
    },
    {
        process: '进行中'
    },
]
let obj = {       
    process: '完成'     
    }     

    for (let index =0; index < list.length;index ++) {
        list[index] = obj
    }
console.log('最终数据:',list)
/* 
最终数据
[ { process: '完成' }, { process: '完成' }, { process: '完成' } ]
*/

你可能感兴趣的:(forEach的问题)