18

Could anyone help explain why length is returning 0? Thank you VERY much in advance!

var errors = [];
errors[''] = "Monday Morning slot already taken. Would you like to replace it?";
console.log(errors);
document.write(errors.length);
Justin Harris
  • 269
  • 1
  • 3
  • 12

5 Answers5

36

Only numeric indices affect the .length of an Array.

Other named properties are allowed, but they aren't the typical use for an Array object. By using "", you're creating a non-numeric property on the object. You can access it like this:

errors[""];

But you can't get to it with the typical Array methods.


For named properties, you'd typically use an Object instead. Either way, you can get a count of the number of own, enumerable properties (including numeric indices) by using Object.keys().

Object.keys(errors).length;
cookie monster
  • 10,671
  • 4
  • 31
  • 45
  • @Derek朕會功夫: The empty string? Yeah, oddly enough that's a valid name. :-) – cookie monster Jan 25 '14 at 22:12
  • This should be the accepted answer, @Derek朕會功夫 answer solved half of the problem (one dimension array), consider the following example: error['A']='message A', errors['B']='message B'...with push solution you would not be able to lookup by keys any more. – 0xFK Mar 23 '19 at 19:19
12

Because that is not how you add an item since Arrays only accept numeric keys. You do it like this:

errors.push("blah");  /*or*/  errors[0] = "blah";

//Now if you check the length:
errors.length;  //1

Also, if you are using it as an Object, '' isn't a valid name either.

*Correction: Looks like you can use "" (empty string) as a key.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 1
    Please edit the answer to note that while you can add items this way, this doesn't affect the length property. It is incorrect to say that you can only accept numeric keys. – Va5ili5 Mar 09 '19 at 19:33
6

As you used string key instead of int, JavaScript will consider your array as an object storing key/value pairs.

as @cookiemonster mentioned you can get length using Object.keys(errors).length;

Also, you may consider using JavaScript ES6 Map.

var errormap = new Map();
errormap.set('err101', 'Description for error 101');
errormap.set('err102', 'Description for error 102');

To get the description of error description based on error key:

console.log(errormap.get('err101'));

You can get the length of the Map object by simply write:

console.log(errormap.size);
0xFK
  • 2,433
  • 32
  • 24
2

Your Array needs a numeric input, you're currently using an empty string as an Index, which is invalid.

You can either specify an index, initialise the Array with your given elements, or push that item to the Array:

// Option 1: Specify index
var errors = [];
errors[0] = 'Monday Morning slot already taken. Would you like to replace it?';

// Options 2: Initialise the Array with values
var errors = ['Monday Morning slot already taken. Would you like to replace it?'];

// Option 3: Push the item to Array
var errors = [];
var myString = 'Monday Morning slot already taken. Would you like to replace it?';
errors.push(myString);
Todd Motto
  • 903
  • 6
  • 10
0

The index of any array starts from zero and when you're trying get the value at a particular index or change a value of a particular index, you specify the index as integer which always starts from zero inside the square bracket.

for example,

let errors = [] // creating a new instance of an array which is empty.

// Adding item to the array can be done in two ways;

  1. erros.push(add any value you like)
  2. errors[0] = 'add any value you like'

both method of adding item to an array will add the item at zero.

for your code to work, use integer inside the [] and not string. I hope you get the gist.