简体   繁体   English

javascript如果比较.shift()到数组的最后一个值不起作用

[英]javascript if compare .shift() to last value of array not working

I have an array with some nested arrays. 我有一个带有一些嵌套数组的数组。 I iterate through the array, and nested arrays with .shift(). 我遍历数组,并使用.shift()嵌套数组。 When the .shift() is equal to the last value in the nested array I iterate through the top level and am logging that with console.log("last member of array"). 当.shift()等于嵌套数组中的最后一个值时,我遍历顶层并使用console.log(“数组的最后一个成员”)记录它。 If the active shift() is not the last value then I log "member of array". 如果活动的shift()不是最后一个值,那么我记录“数组的成员”。

For some reason my if condition to test for the last member of the array isn't working. 由于某种原因,我测试数组的最后一个成员的if条件不起作用。 The weird part is when I log the active shift, and the final value of the array, the log shows they are the same! 奇怪的是当我记录主动移位和数组的最终值时,日志显示它们是相同的! yet the if statement is not running. 但是if语句没有运行。 Not sure if this is because i'm comparing array with object or what. 不确定这是因为我正在将数组与对象或什么进行比较。 Any ideas? 有任何想法吗?

for example in log a3ls is the same as b, which means b = a3ls, yet the block in that if statement isn't running! 例如在log中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

I would think the "b member" should be "last member" per my if statement if ( b == a3ls ). 如果(b == a3ls),我认为“b成员”应该是我的if语句的“最后成员”。

animations is a large array truncated version looks like: 动画是一个大型截断版本看起来像:

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] ]
        ]
    ],
]

nested iteration code: 嵌套迭代代码:

    //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");
             }
        };

iteration code which calls nested iteration if there is nested array: 如果有嵌套数组,则调用嵌套迭代的迭代代码:

    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');
            }

    };

EDIT when I do if(b.toString == a3ls.toString) the test works prints out "last member of array" even when it's not the last member... think it's because the .toString function is the same. 当我执行if(b.toString == a3ls.toString)时, 编辑打印出“数组的最后一个成员”,即使它不是最后一个成员...认为这是因为.toString函数是相同的。

You have the answer to your question in your own code. 您可以在自己的代码中找到问题的答案。

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

a3ls is a COPY of the actual element (slice always returns a new array). a3ls是实际元素的COPY(切片总是返回一个新数组)。 In itersub , b is the actual element. itersubb是实际元素。 Javascript == is based on reference. Javascript ==基于参考。 It doesn't actually check whether the two arrays contains the same elements. 它实际上并不检查两个数组是否包含相同的元素。 It only checks whether the two variables point to the same object, which in this case they don't. 它只检查两个变量是否指向同一个对象,在这种情况下它们不会。

You have two options. 你有两个选择。

    1. Don't make copy. 不要复制。 a3ls = a[3][ a[3].length-1 ]
    1. Implement some external logic that checks whether each element of b exists in a3ls and vice versa. 实现一些外部逻辑,检查b每个元素是否存在于a3ls ,反之亦然。

You might also have a 3rd option. 您可能还有第3个选项。 Don't use shift . 不要使用shift Use a simple for loop and check if current index === length - 1 . 使用一个简单的for循环并检查current index === length - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM