0

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);
                    }
                }
            }
        }

1 Answer 1

1

You don't want delete, you want splice:

topic_array[topic_id].questions.splice(i, 1);
Sign up to request clarification or add additional context in comments.

1 Comment

topic_array[topic_id].questions.splice(i, 1) (splice on the questions array). Many thanks Elliot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.