0

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?

phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
kspacja
  • 4,648
  • 11
  • 38
  • 41
  • That occurs because you incorrectly remove elements. Check this: http://stackoverflow.com/questions/500606/javascript-array-delete-elements – Vladimir Posvistelik Jun 22 '12 at 15:10
  • I read this before to find the best way to remove elements. So I decided to use "splice", but there is no about adding element after removing or I can't see it. If you could, expain what I do wrong? As you can see during removing I can't detect the problem, only when an element is added. – kspacja Jun 22 '12 at 15:19

2 Answers2

1

Either A) -> You're using current element to track the position to next add to, and never decreasing it:

var arr = []
for (var currId = 0; currId < 4; currId++) {
    arr[currId] = currID
}
//arr = [1, 2, 3, 4]
arr.splice(1, 3); // arr = [1]
// if currID is still 3
arr[currId] = "text"; // now arr = [1, undefined, undefined, "text"]

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()

var arr = []
for (var currId = 0; currId < 4; currId++) {
    arr.push(currId);
}
//arr = [1, 2, 3, 4]
arr.splice(1, 3); // arr = [1]
arr.push("text"); // now arr = [1, "text"]

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,

this.list[currId] = text;

you follow it with this:

currId++;

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:

var arr = [1, 2, 3, 4];
currId = 4 // The next position to add at is 4.
removeCount = 3;
arr.splice(1, removeCount); // arr = [1]
currId -= removeCount;

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.

phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
0

What's your currId for?

When adding an element (to the end), you should do this:

this.list.push(text)

or

this.list[this.list.length] = text

and modify the element by index.

iMom0
  • 12,493
  • 3
  • 49
  • 61