如果将compare.shift()与数组的最后一个值比较,则为javascript无效
我有一个带有嵌套数组的数组。我遍历数组,并使用.shift()嵌套数组。当.shift()等于嵌套数组中的最后一个值时,我遍历顶层,并使用console.log(“数组的最后一个成员”)记录该值。如果活动shift()不是最后一个值,则我记录“数组成员” 出于某种原因,要测试数组最后一个成员的if条件不起作用。奇怪的是,当我记录活动移位和数组的最终值时,日志显示它们是相同的!但是if语句没有运行。不确定这是因为我正在比较数组和对象还是什么。有什么想法吗 例如,日志中的a3ls与b相同,这意味着b=a3ls,但if语句中的块没有运行如果将compare.shift()与数组的最后一个值比较,则为javascript无效,javascript,arrays,multidimensional-array,loops,Javascript,Arrays,Multidimensional Array,Loops,我有一个带有嵌套数组的数组。我遍历数组,并使用.shift()嵌套数组。当.shift()等于嵌套数组中的最后一个值时,我遍历顶层,并使用console.log(“数组的最后一个成员”)记录该值。如果活动shift()不是最后一个值,则我记录“数组成员” 出于某种原因,要测试数组最后一个成员的if条件不起作用。奇怪的是,当我记录活动移位和数组的最终值时,日志显示它们是相同的!但是if语句没有运行。不确定这是因为我正在比较数组和对象还是什么。有什么想法吗 例如,日志中的a3ls与b相同,这意味着b
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函数是相同的。您可以在自己的代码中找到问题的答案
a3ls = a[3][ a[3].length-1 ].slice(); //make a copy of the last element of nested array before shift destructs the array
a3ls
是实际元素的副本(slice总是返回一个新数组)。在itersub
中,b
是实际元素。Javascript==
基于引用。它实际上并不检查两个数组是否包含相同的元素。它只检查这两个变量是否指向同一个对象,在本例中,它们不指向同一个对象
你有两个选择
- 不要复制<代码>a3ls=a[3][a[3]。长度-1]
- 执行一些外部逻辑,检查
的每个元素是否存在于b
中,反之亦然a3ls
您可能还有第三个选择。不要使用
shift
。对循环使用一个简单的,并检查当前索引===长度-1
a是:.pcb\u cad\u cfd,fadeIn,1000,
。最后一个逗号(在1000之后)向我指出,这个数组的结尾可能有一个null
/未定义的值,这可能是您没有预料到的。很好,我将对此进行调查!查看我文章末尾的编辑。。。有点事要做,但我不知道为什么!谢谢你的指导。你对副本和==的描述让我了解了一些情况。如果不进行复制,测试就会失败,因为转换是破坏性的,出于某种原因,我认为for循环在没有等待jquery动画完成时出现了问题,但我会再次尝试,并继续使用外部逻辑来检查元素(还不确定如何做)。感谢你的指导和选择;我很感激!把切片去掉是有效的。我发誓我是从那里开始的,我可能在破坏数组的地方重新构造了。谢谢你的帮助。我真的很感激!