The problem is strange: I removed from Array (association) a few elements by splice function, after this I added a few elements and suddenly there were “underfined” elements. Below there is the log (after ‘//’ this my comment to this):
// adding first element; array length = 1
["asdasdasd"] microblog.js:48
// adding second element; array length = 2
["asdasdasd", "asdasdasd"] microblog.js:48
// adding third element; array length = 3
["asdasdasd", "asdasdasd", "asdasdasdqwe"] microblog.js:48
// removing second element; array length = 2
["asdasdasd", "asdasdasdqwe"] microblog.js:66
// removing third element; array length = 1
["asdasdasdqwe"] microblog.js:66
// adding new element; array length = 4
["asdasdasdqwe", undefined × 2, "asdasdqwrevcvxzvvxwfrqeqwewq"]
And this is how I add an element:
(...)
this.list[currId] = text;
(...)
And this is how I remove an element
(...)
this.list.splice(currId, 1);
(...)
How to avoid this undefined elements?
Either A) -> You’re using current element to track the position to next add to, and never decreasing it:
in which case you need to decrease currId when you remove items to add them in the very next place.
Or B) -> just use
Array.push()B is a much better solution, if just adding the element to the end of the array is what you wanted.
EDIT: Since you want to maintain the “currID” as the next position in the Array to place an item at, I assume whenever you do this,
you follow it with this:
If that is correct, then the opposite thing to do would be to decrease it when you splice objects out of the Array e.g:
But frankly, this is an awful way to do this. Please give some more information so that we can help you find a better way.
Hope this helps.