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?

2
  • That occurs because you incorrectly remove elements. Check this: stackoverflow.com/questions/500606/… Commented Jun 22, 2012 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. Commented Jun 22, 2012 at 15:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay B is good option, but I what to use array as hash table. I need to know the key of element, so I can't use push.
Perhaps you should be using an object rather than an array? What exactly are you trying to do?
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.

1 Comment

currId is interger, but I use it as key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.