For example, I have an array of strings, ['12', '34', '56'], and I want to dynamically update this array by adding additional elements ('78', '90'), or by removing some elements ('34', '56').
Not sure if there’s a better data structure/library that gets the job done. Can someone shed some lights for a JS newbie?
To add to an array use
.push.Removing a specific element, may take a bit more work. To to that, you’ll need to copy the array into a new one (without the element(s) you want to remove).
John Resig (creator of jQuery) posted a function to remove array elements by their indexes (which you’ll need to get 1st). His function is here.
Update:
.splicecould also work to remove elements.Note:
Array.indexOfmay not work in all browsers. There is a function that will add it to older browsers at the MDN docs page.