0

我有一个包含一些嵌套数组的数组。我遍历数组,并使用 .shift() 嵌套数组。当 .shift() 等于嵌套数组中的最后一个值时,我会遍历顶层并使用 console.log("last member of array") 记录它。如果活动的 shift() 不是最后一个值,那么我会记录“数组成员”。

出于某种原因,我测试数组最后一个成员的 if 条件不起作用。奇怪的是,当我记录活动班次和数组的最终值时,日志显示它们是相同的!但是 if 语句没有运行。不确定这是否是因为我正在将数组与对象或什么进行比较。有任何想法吗?

例如,在日志中 a3ls 与 b 相同,这意味着 b = a3ls,但该 if 语句中的块没有运行!

a3ls: .wePredOpTemps, .pcb_cad,fadeOut,1000
a is: .pcb_cad_cfd,fadeIn,1000,
b is: .wePredOpTemps, .pcb_cad,fadeOut,1000
a[3] is: 
b member

根据我的 if 语句 if ( b == a3ls ),我认为“b 成员”应该是“最后一个成员”。

动画是一个大数组截断版本,看起来像:

animations = [
    ['.arrows', 'fadeIn', [1000], [
        ['.heatGenComps', 'fadeIn', [1000] ],
        ['.heatGenComps', 'delay', [2000] ],
        ['.heatGenComps, .arrows', 'fadeOut', [1000] ]
        ]
    ],
    ['.pcb_cad_cfd', 'fadeIn', [1000] , [
        ['.wePredOpTemps', 'fadeIn', [1000] ],
        ['.wePredOpTemps', 'delay', [2000] ],
        ['.wePredOpTemps, .pcb_cad', 'fadeOut', [1000] ]
        ]
    ],
]

嵌套迭代代码:

    //iterate through nested array,
        function iterSub( a, a3ls ){
            b = a[3].shift(); //grab active of nested array
            console.log('a3ls: ' + a3ls);
            console.log('a is: ' + a );
            console.log('b is: ' + b);
            console.log('a[3] is: ' + a[3]);

            if ( b )
            {
                animations.push(b); // add active value back to animations array for infinite loop
                if ( b == a3ls ) //if active is last of array then
                {
                    console.log("last member of b array");
//run animations with promise and done then call top level array iterations
                    $.fn[ b[1] ].apply( $( b[0] ), b[2] ).promise().done( function(){
                        iter();     
                    });

                }
                else //more members left to iterate 
                {
                    console.log("b member");
//run animations and call sub with destructed array and the copy of the last value
                    $.fn[ b[1] ].apply( $( b[0] ), b[2] );
                    iterSub( a, a3ls );
                }

             }
             else //no more elements left
             {
                console.log("b does not exists");
             }
        };

如果存在嵌套数组,则调用嵌套迭代的迭代代码:

    function iter(){

        if (!animating) return;

            a = animations.shift(); //active array

            if (a) //if there is something, do something
            {

                console.log("a exists"); //log a
                animations.push(a); //put a back to the bottom of array for infinite loop

                 if ( a[3] ) //if there is a nested array then
                 {
                    a3ls = a[3][ a[3].length-1 ].slice(); //make a copy of the last element of nested array before shift destructs the array
                    console.log("a[3] exists");
                    $.fn[ a[1] ].apply( $( a[0] ), a[2] ); //applied some jquery animations based on array parameters
                    iterSub( a, a3ls  ); //call function to iterate through nested array and pass it the a array and a copy of the nested array's last value.
                 }
                 else //no nested array then do this
                 {
                    console.log("a[3] does not exist");
//use array to run jquery animations, promise, done, iterate to next 
                    $.fn[ a[1] ].apply( $( a[0] ), a[2] ).promise().done( function(){
                        iter();
                    });
                 }
            }
            else //no a, nothing to do
            {
                return alert('a does not exist');
            }

    };

编辑 当我做if(b.toString == a3ls.toString)测试工作时会打印出“数组的最后一个成员”,即使它不是最后一个成员......认为这是因为 .toString 函数是相同的。

4

1 回答 1

2

您可以在自己的代码中找到问题的答案。

a3ls = a[3][ a[3].length-1 ].slice(); //make a copy of the last element of nested array before shift destructs the array

a3ls是实际元素的副本(切片总是返回一个新数组)。在itersub中,b是实际元素。Javascript==基于参考。它实际上并不检查两个数组是否包含相同的元素。它只检查两个变量是否指向同一个对象,在这种情况下它们没有。

你有两个选择。

    1. 不要复制。a3ls = a[3][ a[3].length-1 ]
    1. 实现一些外部逻辑,检查每个元素是否b存在,a3ls反之亦然。

您可能还有第三种选择。不要使用shift. 使用一个简单的for循环并检查 if current index === length - 1

于 2012-11-24T17:09:40.683 回答