简体   繁体   English

Javascript数组拼接无法正常工作

[英]Javascript Array Splice Not working fine

var cache = [];
cache[0] = "0";
cache[1] = "1";
cache[2] = "2";
cache[3] = "3";
cache[4] = "4";
cache["r"] = "r";
console.log(cache.length);
for(key in cache){
    if(isNaN(key))continue;
    else cache.splice(key,1); // cache.splice(key) is working fine, ***
}
console.log(cache);

Question : in line *** Why splice(key) is working fine (Deleting All Elements with Numeric Index) and splice(key,1) not working fine (Not Deleting Elements with Numeric index). 问题:在***行中,为什么splice(key)可以正常工作(删除带有数字索引的所有元素),而splice(key,1)不能正常工作(不删除带有数字索引的元素)。 Even i have tried 即使我尝试过

splice(key,1) // Not working as splice(key)
splice(key--,1) // Even not working as splice(key)
splice(key,0) // not deleting any thing

You can copy and paste code in Firebug console for testing. 您可以在Firebug控制台中复制和粘贴代码以进行测试。

It's not working because you are removing items from the array whil looping through the keys. 它不起作用,因为在循环遍历键时要从数组中删除项目。 When you remove an item, it will rearrange the other items depending on how the array is implemented internally, and you end up with a loop that doesn't iterate over the keys that you expect. 当您删除一个项目时,它将根据内部实现数组的方式重新排列其他项目,最终您将得到一个循环,该循环不会迭代您期望的键。

When I try it in Firefox, it only iterates over the keys 0 , 1 , 2 and r . 当我尝试在Firefox,它只遍历键012r Removing items while iterating makes it skip 3 and 4 . 迭代时删除项目将使其跳过34 The splice itself works fine, but it affects the loop so that some items are simply not in the iteration. splice本身可以正常工作,但是会影响循环,因此某些项目根本不在迭代中。

As you are actually looking for the indexes in the array by skipping non-numerical keys, you can just loop through the indexes instead. 当您实际上通过跳过非数字键在数组中查找索引时,可以直接遍历索引。 By looping through them backwards, you don't get the problem with the array changing while you loop through it: 通过向后循环遍历它们,您不会在遍历数组时遇到数组更改的问题:

var cache = ["0", "1", "2", "3", "4"];
cache.r = "r";
console.log(cache.length);
for (var i = cache.length - 1; i >= 0; i--) {
    cache.splice(i, 1);
}
console.log(cache);

Demo: http://jsfiddle.net/CguTp/1/ 演示: http//jsfiddle.net/CguTp/1/

1) cache["r"] = "r"; 1) cache["r"] = "r"; does not add an element to your array, it adds a property to the cache object 不会将元素添加到数组,而是将属性添加到缓存对象

To initialize an array you can use some thing like 要初始化数组,您可以使用类似

var cache = ["0", "1", "2", "3", "4", "r"];

or 要么

var cache = new Array();
cache.push("0");
cache.push("1");
cache.push("2");
cache.push("3");
cache.push("4");
cache.push("r");

Since your cache object is not an array, you cannot expect splice to behave as it would for an array. 由于您的缓存对象不是数组,因此不能期望拼接行为像数组一样。

2) splice expects an index as the first argument, not a key 2)拼接期望索引是第一个参数,而不是键

see http://www.w3schools.com/jsref/jsref_splice.asp 看到http://www.w3schools.com/jsref/jsref_splice.asp

So you could use this to remove all numeric values: 因此,您可以使用它删除所有数字值:

for (var i = 0; i < cache.length; i++) {
        if (!isNaN(cache[i])) {
            cache.splice(i, 1); // cache.splice(key) is working fine, ***
                i--;
            }
        }

Splice expects first index as numeric, Splice希望第一个索引为数字,

splice(n,x); //n and x are numeric here  

It will start removing values from array starting at index n and remove x values after index n. 它将开始从索引n开始的数组中删除值,并删除索引n之后的x值。

Now if n is not numeric but a key then no need of x because x can move pointer forward in a numeric-indexed array only not associative array. 现在, if n is not numeric but a key则不需要x,因为x只能在numeric-indexed array向前移动指针,而不是关联数组。 So removing x from splice(n,x) will make function similar to splice(key) etc and so it will work fine. 因此,从splice(n,x)中删除x会使函数类似于splice(key)等,因此可以正常工作。

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

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