Javascript 数组拼接无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17429764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Array Splice Not working fine
提问by Wasim A.
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). Even i have tried
问题:***
为什么 splice(key) 工作正常(删除带有数字索引的所有元素)和 splice(key,1) 工作不正常(不删除带有数字索引的元素)。即使我已经尝试过
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 控制台中复制和粘贴代码以进行测试。
采纳答案by Wasim A.
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. So removing x from splice(n,x) will make function similar to splice(key) etc and so it will work fine.
现在if n is not numeric but a key
不需要 x 因为 x 可以在numeric-indexed array
唯一的非关联数组中向前移动指针。因此,从 splice(n,x) 中删除 x 将使函数类似于 splice(key) 等,因此它可以正常工作。
回答by Guffa
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
. Removing items while iterating makes it skip 3
and 4
. The splice
itself works fine, but it affects the loop so that some items are simply not in the iteration.
当我尝试在Firefox,它只遍历键0
,1
,2
和r
。在迭代时删除项目使其跳过3
和4
。在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);
回答by Eric Beaulieu
1) cache["r"] = "r";
does not add an element to your array, it adds a property to the cache object
1)cache["r"] = "r";
不向数组添加元素,而是向缓存对象添加属性
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.
由于您的缓存对象不是数组,因此您不能期望 splice 的行为与数组相同。
2) splice expects an index as the first argument, not a key
2) splice 需要一个索引作为第一个参数,而不是一个键
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--;
}
}