Forum Moderators: open

Message Too Old, No Replies

Deleting entire index from JSON array

         

csdude55

3:07 am on Apr 18, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a JSON array that looks like this in the console:

0 {"name":"example.jpg", "uuid":"blah-blah-blah", "thumbnailUrl":"/fineuploader/fles/blah-blah-blah/example.jpg"},
1 {"name":"example_2.jpg", "uuid":"blah2-blah2-blah2", "thumbnailUrl":"/fineuploader/fles/blah2-blah2-blah2/example_2.jpg"},
2 {"name":"example_3.jpg", "uuid":"blah3-blah3-blah3", "thumbnailUrl":"/fineuploader/fles/blah3-blah3-blah3/example_3.jpg"}


I need the user to be able to be able to dynamically remove an index... meaning, they click to remove the second one, and the array becomes:

0 {"name":"example.jpg", "uuid":"blah-blah-blah", "thumbnailUrl":"/fineuploader/fles/blah-blah-blah/example.jpg"},
2 {"name":"example_3.jpg", "uuid":"blah3-blah3-blah3", "thumbnailUrl":"/fineuploader/fles/blah3-blah3-blah3/example_3.jpg"}


I keep finding out to delete a single element out of a JSON object, but not how to delete the entire index... maybe I'm using the wrong word to describe it?

NickMNS

4:04 am on Apr 18, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this what you want?
jsonArray.splice(index, 1)

csdude55

4:55 am on Apr 18, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're batting 1000 tonight, my friend :-) It doesn't do EXACTLY what I want since it reorders the index (instead of 0 and 2 I have 0 and 1), but I think I can still make it work.

Thanks!

csdude55

6:16 am on Apr 19, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, update on that... it turns out that I have no choice but to keep the original index, so using array.splice() doesn't work out so well, after all.

I found that I can use this:

delete array[1];


That seems to work flawlessly, but I've read a lot of negative comments; it doesn’t free the memory allocated to the element, so there's a possibility of overflow?

I suspect that it will be VERY rare for there to be more than 20 indexes for any user... it's more likely for there to be 1 or 2. So is this really anything I should worry about?

Fotiman

2:24 pm on Apr 19, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think delete is what you'd want to use. I'm not aware of how that might create a memory leak, considering your array example contains JSON objects and not anything with a DOM reference.
Can you link to a reference stating the overflow issue you mentioned?

Additionally, you could also use splice to remove the element from the array, and insert an undefined in it's place:

jsonArray.splice(index, 1, undefined)

NickMNS

4:40 pm on Apr 19, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Instead of naively selecting the index to delete, ie: delete index 2, because it is the second entry, use a unique identifier such as 'uuid' then use find.index to find the index of the record.
See this:
[stackoverflow.com...]

csdude55

10:51 pm on Apr 19, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not aware of how that might create a memory leak, considering your array example contains JSON objects and not anything with a DOM reference.
Can you link to a reference stating the overflow issue you mentioned?

I looked at a lot of pages, but I think this is the one that's sticking in my mind:

[stackoverflow.com...]

This also stuck in my head; it's about remove instead of delete, but it seems to do the same thing so I wasn't sure if the same memory leak would apply:

[arduinojson.org...]

Instead of naively selecting the index to delete, ie: delete index 2, because it is the second entry, use a unique identifier such as 'uuid' then use find.index to find the index of the record.

Good thought, Nick. I'm 99% sure that the script I'm working with (Fine Uploader) will be consistent on the index, so I'm not sure if it's worth the extra few milliseconds to do a find.index. I'll keep it in mind, though, in case any problems arise during beta testing.