我有一个remove []数组,它包含数据数组中所有元素的所有索引位置(如下所示)。
数据阵列:
Retail,1,Utilities,1,Food & Restaurant,3,No Data,4,Construction,0,Non-profit,1,Financial Services,12,Technology,2,Law,3,Religion,3,Retired,2,Insurance,0,Real Estate,2,Audit,3,Business Organizations,3,Media & Marketing,0,Education,3,Transportation,0,Manufacturing,0,Entertainment & Sports,0,Architecture & Engineering,0,Cultural Institutions,0,Government,0,Banking,0,Health Care,0,Business Services,0
我的javascript
var remove = [];
$.each(options.series[0].data, function(index, item) {
if (options.series[0].data[index][1] == 0)
{
//options.series[0].data.splice(index,1);
remove[index] = index;
}
for (i=0; i<=remove.length; i++)
{
//alert(remove);
if (remove[i] != undefined)
options.series[0].data.splice(remove[i],1);
}
splice()之后的数据数组。很多带0的元素仍然存在。
Retail,1,Utilities,1,Food & Restaurant,3,No Data,4,Non-profit,1,Financial Services,12,Technology,2,Law,3,Religion,3,Retired,2,Insurance,0,Audit,3,Business Organizations,3,Media & Marketing,0,Education,3,Manufacturing,0,Entertainment & Sports,0,Cultural Institutions,0,Banking,0,Business Services,0
如果我将拼接更改为包含替换元素
options.series[0].data.splice(remove[i],1,'removed');
从数据数组中删除所有0个元素。是吧?
Retail,1,Utilities,1,Food & Restaurant,3,No Data,4,removed,Non-profit,1,Financial Services,12,Technology,2,Law,3,Religion,3,Retired,2,removed,Real Estate,2,Audit,3,Business Organizations,3,removed,Education,3,removed,removed,removed,removed,removed,removed,removed,removed,removed
如何删除数据数组中的所有0个元素?
答案 0 :(得分:15)
每次删除一个,你都会使任何进一步的缓存索引过时,因为从那一点开始的数组被重新编制索引。
因此,您在第一个项目之后删除了错误的项目。
您应该反过来迭代remove
数组。
var i = remove.length;
while (i--) {
if (remove[i] != undefined)
options.series[0].data.splice(remove[i],1);
}
此外,如果您只是undefined
.push()
数组中的每个索引,就可以提高性能并摆脱remove
测试...
remove.push(index);
答案 1 :(得分:0)
如果查看原始数组和第一个结果数组,您会注意到正在删除非0元素。我可以告诉你的问题是你的数组remove
正在寻找那些在删除之前的数据后从左移一个的索引。您需要在最终的for
循环中为此添加更正调整,或者采用另一种方法。
答案 2 :(得分:0)
如果要删除数组中的所有元素,最好不要使用splice
,而是使用length
:
options.series[0].data.length = 0;
请参阅:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/length
更新
我误解了这个问题。在你的情况下,我可能会过滤掉零的元素,而不是制作两个循环(一个用于收集它们,一个用于删除它们)。所以,像:
function isNotZero(item) {return item[1] !== 0}
var filtered = options.series[0].data.filter(isNotZero);
options.series[0].data = filtered;
请参阅:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
如果您要支持的浏览器没有实现ES5,我建议您使用垫片来处理所有es5方法(在该页面中有一个用于过滤器)或库underscore < / p>
答案 3 :(得分:0)
我认为你可以通过这种方式快速完成:
var dataArr = ["Retail",1,"Utilities",1,"Food & Restaurant",3,"No Data",4,"Construction",0,"Non-profit",1,"Financial Services",12,"Technology",2,"Law",3,"Religion",3,"Retired",2,"Insurance",0,"Real Estate",2,"Audit",3,"Business Organizations",3,"Media & Marketing",0,"Education",3,"Transportation",0,"Manufacturing",0,"Entertainment & Sports",0,"Architecture & Engineering",0,"Cultural Institutions",0,"Government",0,"Banking",0,"Health Care",0,"Business Services",0];
var item = 0; // value of the item to remove
while (dataArr.indexOf(item) > -1) {
dataArr.splice(dataArr.indexOf(item), 1);
}
console.log(dataArr);
答案 4 :(得分:0)
我的问题是splice正在从数组中删除错误的元素。我用while循环看到了答案,但我做的解决方案是:
var index = jQuery.inArray(element, arr);
arr.splice(index,1);
答案 5 :(得分:0)
我使用一个名为DevBox.Linq.JS的库
它基本上将Linq类型的功能添加到Javascript数组。例如,它添加了“删除”:
Array.prototype.Remove = function (condition) {
if (condition == undefined) {
console.error('Informe a expressão para executar a tarefa.')
return;
}
for (var i = 0 ; i < this.length ; i++) {
if (condition(this[i]))
this.splice(i, 1);
}
}
这样,您可以将其与如下所示的lambda一起使用:
myArray.remove(i=>i=="valueToDelete");
希望它会有所帮助,它已经具有所有功能,因此您不必处理讨厌的接头。