Javascript array.splice () not removing element in array?
I have an array remove [] that has all the index positions of the entire element with 0 in the data array (as shown below).
data array:
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
my 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);
}
array of data after splice (). Many elements with 0 are still there.
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
if i change splicing to replacement element
options.series[0].data.splice(remove[i],1,'removed');
All 0 elements are removed from the data array. Yes?
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
How do I remove all 0 elements in my data array?
source to share
Every time you delete one of them, you make any other cached indexes obsolete, because the array is reindexed from that point forward.
As a result, you are removing the wrong items after the first one.
You have to iterate over the array remove
in reverse order.
var i = remove.length;
while (i--) {
if (remove[i] != undefined)
options.series[0].data.splice(remove[i],1);
}
Also, you can improve performance and get rid of the test undefined
if just .push()
each index into an array remove
...
remove.push(index);
If you look at your original array and first result array, you will notice that the non-0 ARE elements have been removed. The problem is, I can tell that your array is remove
looking for indices that have since been shifted over one to the left while deleting the previous ones. You will need to add an adjustment for this in your last cycle, for
or go this other way.
source to share
If you want to remove all elements from an array, it is better not to use splice
, but rather length
:
options.series[0].data.length = 0;
See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/length
Update:
I misunderstood the question. In your case, I'll probably filter out the items that are zero instead of two loops (one to collect them and one to remove them). So something like:
function isNotZero(item) {return item[1] !== 0}
var filtered = options.series[0].data.filter(isNotZero);
options.series[0].data = filtered;
See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
If the browser you want to support does not implement ES5 then I suggest you use a shim for all es5 methods (there is one for a filter on this page) or a library like underscore
source to share
I think you can do it pretty quickly:
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);
source to share