Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  82] [ 4]  / answers: 1 / hits: 38436  / 11 Years ago, tue, july 2, 2013, 12:00:00
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,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.


More From » jquery

 Answers
25

Splice expects first index as numeric,



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.



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.


[#77253] Monday, July 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dillionsalvadorg

Total Points: 288
Total Questions: 103
Total Answers: 75

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;