JavaScript不会向数组添加值

马库斯·鲁迪克(Marcus Ruddick)

我写了一个返回值的脚本。当我记录包含值的变量时,它将显示它们,并且它们是正确的。然后,无论我如何尝试,当我尝试将值添加到数组中时,进行concat,push,equals等操作,然后立即记录该数组的内容,这些值不存在或未定义。这是我的代码,或者至少是它的一个子集。

displayRange: function () {
    if( app.selectedObject !== undefined && app.selectedObject.movement !== undefined && app.selectActive === undefined ) {

        // holds rage
        t.rArray = [];
        t.oArray = [];
        app.selectActive = true;
        t.obj = app.selectedObject;

        // amount of allotted movement for unit
        t.len = t.obj.movement * 2;

        // loop through x and y axis
        for ( x = -t.len; x < t.len; x += 1){
            for ( y = -t.len; y < t.len; y += 1 ){

                // return cost of each movement
                t.hl = this.calcRange('x', x, y );

                // if movement cost is less then or eual to the allotted movement then add it to the range array
                if ( t.hl.move <= t.obj.movement ){
                    // find obsticles

                    t.ex = t.hl.origin.x + x;
                    t.wy = t.hl.origin.y + y;
                    t.obsticle = this.findObsticles(t.ex, t.wy);        

                    // get the number of offset movement from the obsticle
                    if ( t.obsticle !== undefined ){
                        t.offset = app.settings.obsticleStats[t.obsticle.obsticle][app.selectedObject['type']];
                        if ( t.offset !== undefined ){
                            // make an array of offset values, starting point, plus movement, and the amount of offset beyond that movement
                            t.oArray.push(this.obsticleLogic( t.ex, t.wy, t.mx, t.my, t.offset ));
                        }
                    }
                    // add all values to array
                    t.rArray.push({ x: t.ex, y: t.wy, type:'highlight'});
                }
            }
        }

        offsetArr = app.offsetArray( t.rArray, t.oArray );
        for ( l = 0; l < offsetArr; l += 1 ){
            app.map.highlight.push(offsetArr[l]);
        }

        for ( i = 0; i < t.oArray.length; i += 1 ){
            app.offset.push(t.oArray[x]);
        }

        console.log(t.oArray);
        console.log(offsetArr);
        console.log('offset:');
        console.log(app.offset);
        console.log('highlight');
        console.log(app.map.highlight);

        window.requestAnimationFrame(app.animateEffects);
    }

    return false;
},

我已经设置了app.map.highlight = []; 在app.map区域中。与app.offset相同。

它们已经被声明,它们只是在另一个对象中。

我不明白为什么它不会将值添加到数组。我记录了它们并看到了它们,然后在调用数组推送之后立即将其设置为未定义。为什么会这样?

它们被循环遍历以尝试将它们放入数组的唯一原因是因为这是我最后尝试过的方法。

马库斯·鲁迪克(Marcus Ruddick)

我相信我遇到了变量名冲突的问题,我将其中一些重命名,然后它开始工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

向作为参数传递的数组添加值不会为其添加值

来自分类Dev

向动态数组添加值

来自分类Dev

向JS数组添加值

来自分类Dev

从Android中的Multiautocomplete向数组添加值

来自分类Dev

向多维数组的每个元素添加值

来自分类Dev

向JSON数组对象添加值

来自分类Dev

向MySQL结果数组添加值

来自分类Dev

向现有数组添加值

来自分类Dev

Foreach 内部 foreach 向数组添加值

来自分类Dev

在JavaScript中从数组添加值

来自分类Dev

Android SQLite不会向数据库添加值

来自分类Dev

添加以向数组内的对象添加值

来自分类Dev

如果外部数组的名称=='something',则向JSON数组添加值

来自分类Dev

如何在单击事件上向数组添加值

来自分类Dev

向不存在的数组元素添加值

来自分类Dev

如何在GUI中向数组添加值

来自分类Dev

向数组添加值后无法使函数正常工作

来自分类Dev

向数组中未知数量的元素添加值

来自分类Dev

反应事件处理程序。向数组添加值

来自分类Dev

尝试向数组添加值时获取NullReferenceException

来自分类Dev

向数组添加值后无法使函数正常工作

来自分类Dev

在PHP中向数组添加值的最快方法是什么

来自分类Dev

如何在jQuery中向数组添加值?

来自分类Dev

从适配器向模型数组对象添加值

来自分类Dev

向结构数组添加值会导致段错误

来自分类Dev

如何向现有数组元素添加值?

来自分类Dev

如何使用Javascript向文本框添加值

来自分类Dev

向JavaScript数组添加名称

来自分类Dev

从按钮向UILabel添加值