Remove element from array using Javascript
I have one javascript array, I will store this array in local storage
var result;
result = [1,2,3,4,5];
localStorage.setItem('result', JSON.stringify(result));
Above is the result of the array and I have set the values โโof the array to local storage
function removeItem(Id){
result= JSON.parse(localStorage.getItem('result'));// get array values from local Strorage
var index = result.indexOf(Id);// find index position
result.splice(index , 1); //and removing the Id from array
localStorage.setItem('result', JSON.stringify(result));// result set to local storage
}
function call
var id = 1;
removeItem(id);
The first positioned value of the array is not removed from the array elements. All other values โโwill be completely removed with this function. But the first value in the array is not removed from the array. Can anyone suggest a better option?
+3
source to share
3 answers