-3

So...I have this array:

val['an_element'][0]['another_element'][2]['text']

I want to get rid of the entire "2" node.

Now...I THOUGHT the way to do this would be:

delete val['an_element'][0]['another_element'][2];

BUT...it doesn't actually drop the element, but simply empties it out.

I also tried:

val['an_element'][0]['another_element'][2] = null;

...but that just resulted in my console log nearly bleeding it was so red with errors.

Basically, i want that [2] node to NO LONGER EXIST. Basically, I want it to NOT BE FOUND AT ALL.

What do I do??? And I know the ".splice" method will NOT actually modify the original array, so please don't suggest it. :)

4

1 Answer 1

3

The splice method will, in fact, modify the array. Just try:

val['an_element'][0]['another_element'].splice(2, 1);

From the docs:

Changes the content of an array, adding new elements while removing old elements.
...
If you specify a different number of elements to insert than the number you're removing, the array will have a different length at the end of the call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.