I'm trying to remove an object from an array with
delete array[index]
which is deleting the object from the array however the .length property of the array is still == 1
Any ideas?
PS I'm trying to delete a question e.g. topic->questions[question_id]
enter image description here
if(topic_array[topic_id] !== 'undefined'){
if(topic_array[topic_id].questions.length > 0){
for(var i = 0; i < topic_array[topic_id].questions.length; i++){
if(topic_array[topic_id].questions[i].question_id == question_id){
delete topic_array[topic_id].questions[i];
console.log(topic_array);
}
}
}
}
asked Jul 21, 2012 at 14:36
Garbit
6,0946 gold badges43 silver badges78 bronze badges
1 Answer 1
You don't want delete, you want splice:
topic_array[topic_id].questions.splice(i, 1);
answered Jul 21, 2012 at 14:37
Elliot Bonneville
53.6k24 gold badges101 silver badges125 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Garbit
topic_array[topic_id].questions.splice(i, 1) (splice on the questions array). Many thanks Elliot
lang-js