3

If entries are being added and removed in an array (not pushed/popped) what is the optimal way to scan and find the first undefined element, so that it can be set with a new value?

3
  • check undefined during insert -_- ..then you can live undefined free life Commented Nov 6, 2015 at 16:59
  • could you show what the array looks like Commented Nov 6, 2015 at 17:00
  • Related: how to know if an array has any defined elements in it? (any that are not undefined) Commented Nov 6, 2015 at 17:03

6 Answers 6

3

If the array contains holes with findIndex you can find the first one like so:

[1, 2,, 4, 5].findIndex(Object.is.bind(null, undefined))

indexOf will ignore holes. So

[1, 2,, 4, 5].indexOf(undefined)

Always returns -1.

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

1 Comment

I tested this with an array of length 4, but having only undefined values as a content, its not working in this case, using an array created by new Array(4) for example. (tested inside an angular 5 demo app)
1

Assuming we have no prior knowledge of operations being performed on the array, the fastest way is to simply iterate through the entire array linearly

var arr = [1, 2, , 4];

for (var i = 0; i < arr.length; i++) {
  if (typeof arr[i] === 'undefined') {
    arr[i] = 'foo';
    break;
  }
}

Or we can keep track of whats being removed using something like this

var earliestRemoved;

if (newRemoved < earliestRemoved || !earliestRemoved) {
  earliestRemoved = newRemoved;
}

Comments

0

I guess the simplest way would be to use Array.indexOf to find the index of the first undefined element.

1 Comment

indexOf ignores holes. So no.
0

Today I've got the same question, so here's my take on it.

It all depends on your specific problem. My problem was this:

  • I have an array instances[], with objects in it;
  • Methods of some of these are registered as callbacks elsewhere;
  • Sometimes I do need to remove some instance from an array;
  • Sometimes I need to add new instance;

The problem is that I can't just splice an array, because it'll invalidate all my callback registrations. Well, I can just leave them in the array and forget, adding new elements at the end. But it's.... bad. It's leaking memory.

So I came with an idea of making "holes" in an array in place of removed elements, and then re-using these holes for new elements.

Thus the question "how to find undefined elements in an array?".

Someone here suggested using array.indexOf(undefined) - unfortunately that won't work... for some reason.

But, if it's okay in your case to use null or 0 instead, then it will work.

Here's an example:

var A = [1,1,1,1, , ,0,0,null,1,1];
A.indexOf(undefined);   // output: -1 (doesn't work)
A.indexOf(0);           // output: 6
A.indexOf(null);        // output: 8

Hope this'll be useful.

Comments

0

With the hindsight of about 4.3 years behind me, What I'd do now is, since I'm the owner of the array, is to make an object with 2 arrays in it, the 2nd just contains the indexes that are cleared out. (prob. manage it in a class). So always have clear set with knowledge of what's undefined. This to avoid iterating through arrays linearly.

Comments

0

2021

Nowadays you can safely use:

myArray.findIndex((element) => (typeof element === "undefined"));

Comments

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.