array.push只是for循环中的最后一个变量javascript

时间:2016-09-21 13:49:50

标签: javascript frontend

我实际上问自己为什么以下代码无法正常工作我找到了解决方案,但它有点棘手,我不喜欢这个解决方案

以下是代码和问题:

    function powerSet( list ){
        var set = [],
            listSize = list.length,
            combinationsCount = (1 << listSize),
            combination;

        for (var i = 1; i < combinationsCount ; i++ ){
            var combination = [];
            for (var j=0;j<listSize;j++){
                if ((i & (1 << j))){
                    combination.push(list[j]);
                }
            }
            set.push(combination);
        }
        return set;
    }

    function getDataChartSpe(map) {
        var res = {};

        for (var i in map) {
            console.log("\n\n");
            var dataSpe = {certif: false,
                experience: 0,
                expert: false,
                grade: 1,
                last: 100,
                name: undefined 
            };
            var compMatchList = [];
            for (var j in map[i].comps_match) {
                var tmp = map[i].comps_match[j];
                compMatchList.push(tmp.name)
            }
            var tmpList = powerSet(compMatchList);
            var lol = [];
            lol.push(map[i].comps_match);
            for (elem in tmpList) {
                console.log("mdr elem === " + elem + " tmplist === " + tmpList);
                var tmp = tmpList[elem];
                dataSpe.name = tmpList[elem].join(" ");
                lol[0].push(dataSpe);
            }
            console.log(lol);
        }
        return res; 
    }

现在这里仍然是相同的代码,但运作良好:

    function powerSet( list ){
        var set = [],
            listSize = list.length,
            combinationsCount = (1 << listSize),
            combination;

        for (var i = 1; i < combinationsCount ; i++ ){
            var combination = [];
            for (var j=0;j<listSize;j++){
                if ((i & (1 << j))){
                    combination.push(list[j]);
                }
            }
            set.push(combination);
        }
        return set;
    }


    function getDataChartSpe(map) {
        var res = {};
        var mapBis = JSON.parse(JSON.stringify(map));

        for (var i in map) {
            var compMatchList = [];
            for (var j in map[i].comps_match) {
               var tmp = map[i].comps_match[j];
               compMatchList.push(tmp.name)
           }
           var tmpList = powerSet(compMatchList);
           mapBis[i].comps_match = [];
           for (elem in tmpList) {
               tmpList[elem].sort();
               mapBis[i].comps_match.push({certif: false,
                   experience: 0,
                   expert: false,
                   grade: 1,
                   last: 100,
                   name: tmpList[elem].join(", ")});
           }
       }
       return mapBis;
    }

实际上它对我来说有点失望,因为它完全相同,但第一个不起作用,第二个起作用。

所以,如果有人能帮助我理解我做错了什么,那就很高兴

ps:对不起,如果我的英语有点破碎

1 个答案:

答案 0 :(得分:0)

在第一个版本中,您构建一个 dataSpe对象并反复使用它。每次运行:

            lol[0].push(dataSpe);

您正在将对同一个对象的引用推送到数组上。

该函数的第二个版本有效,因为它每次都构建一个 new 对象:

           mapBis[i].comps_match.push({certif: false,
               experience: 0,
               expert: false,
               grade: 1,
               last: 100,
               name: tmpList[elem].join(", ")});

传递给.push()的对象文字将在每次代码运行时创建一个新的,不同的对象。