-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. :)

dauber
  • 330
  • 6
  • 20
  • 4
    splice is a Mutator function, it will change the original array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice – compid Jun 19 '13 at 23:00
  • So your array actually is `val['an_element'][0]['another_element']`, containing objects with `text` properties? – Bergi Jun 19 '13 at 23:01
  • possible duplicate of [JavaScript array slice versus delete](http://stackoverflow.com/questions/10325026/javascript-array-slice-versus-delete), and notice the difference between `slice` and `splice`! – Bergi Jun 19 '13 at 23:04
  • 1
    possible duplicate of [JavaScript Array Delete Elements](http://stackoverflow.com/questions/500606/javascript-array-delete-elements) – Felix Kling Jun 19 '13 at 23:05

1 Answers1

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521