3
var array = [];

var object = {};

Now, I need an array of empty objects.

array[0] = {};
array[1] = {};
//........
//........

and

var array = [{}];

is obviously not right.

How to define array of (empty) objects in JS?

var array = [{},{},{},{},.........];

Thanks.

EDIT:

The reason I need is the same reason that having an array with undefined length is useful, and mathematically natural in some cases.

I need object wrappers, and for initialization, it's just an empty object, like some values are null or undefined in many cases.

so, I'll have

var object = {};

array[0].value = 'foo', array[1].value = 'bar'.....

However, I do need multiple object wrappers, and the number is not pre-determined, so consequently, I need an arrays of the objects.

So, I am sorry, I modify my Question title

JavaScript How to define an array of undefined length of empty objects?

7
  • 2
    you just need to declare an empty array and push an object int o it like array.push(object).. Commented Feb 2, 2014 at 4:53
  • 2
    whats wrong with var array = [{}]; that will give you an array containing one empty object? Commented Feb 2, 2014 at 4:56
  • There is no problem writing var array = [{}, {}, {}]; to make an array of objects. Commented Feb 2, 2014 at 4:58
  • why would you need that? I'm curious about the engineering protocole behind that Commented Feb 2, 2014 at 5:00
  • Sebas, good quesion, I edit my Q. Commented Feb 2, 2014 at 5:04

4 Answers 4

3

Arrays can grow and shrink dynamically. So from a certain point of view, they are already of undefined length. You can always add new objects to it if you want to.

You can also create a helper function which checks first if an object exists at a certain position and if not, creates a new one.

You mentioned array[2].value = 'foo' as an example. Here is a helper function that you could use:

function getObjectAtIndex(arr, index) {
    return arr[index] || (arr[index] = {});
}

and then, instead of writing array[2].value = 'foo', you'd write:

getObjectAtIndex(array, 2).value = 'foo'
Sign up to request clarification or add additional context in comments.

4 Comments

Nice. I though that using the literal notation didn't affect the length property.
You mean arr[index]? It does, the length is always set to the highest index + 1.
That's what I meant. I thought that length was updated only when using Array methods :/
Thanks, Felix Kling. I think this is the only way to define(or supply by the helper function) an array of undefined length of empty objects. Your answer is checked.
3

In your array, pushing object in any way is right. That's totally ok. like:

var arr = [];
arr.push({}); // ok
aa[1] = {} // also ok

or:

var arr = [{}, {}, {}]; // the way you are doing is also fine

Or, if you want to do it via loop, that is also fine. Like:

var arr = [];
for(var i=0; i<5; i++){
    arr[i] = {};
}

3 Comments

Well, I agree especially, the last code would be chosen if I know nothing else to do, however, my question is that is it possible to do that for undefined length of array. On second thought, probably it's not possible.
length of an array can not be undefined. Either it would be 0 or a fixed length. You can get the length of the array anytime, right?
Thanks, please check out the answer I've approved. That is what I meant.
1

In general, the way to write

var array = [];
array[0] = {};
array[1] = {};
//........
//........

in a sane manner without lots of copy paste is to use a for loop:

var array = [];
for(var i=0; i<10; i++){
  array[i] = {};
}

Comments

0

I'm pretty sure that an array like var array = []; is an array of Objects. Based on how Javascript works, you never have to worry about null-pointer exceptions, so just define the array and act like you initialized it with the empty objects and see if it works

3 Comments

Thanks, but for instance, if I do var array = []; , I can not add an property like array[2].value = 'foo'
why would you want to use ".value" ? Just do array[2]='foo' Note you could also do array.hey='foo' jsfiddle.net/Kkf6t/1
Because, as I mentioned, the object wrapper is the purpose to implement. Pleaser refer my Q.

Your Answer

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