0

I was surprised that this works in JavaScript. My questions is, is this good practice?

var contact = [];
contact[0] = {};
contact[0].name = "Peter Parker";
console.log(contact[0].name);
Sean3z
  • 3,745
  • 6
  • 26
  • 33
Jason210
  • 2,990
  • 2
  • 17
  • 18

3 Answers3

1

Which part? Setting 0 of contact is generally not good practice when the array is empty - instead, you should make an object, then push that object into the array.

On the other hand, if you're talking about setting a property of contact[0] - yep! That's the entire point of objects - you can think of them similarly to Python dictionaries, except you don't need to use bracket notation to access it.

You can also define the names right inside of the object:

var contact = {name: "Peter Parker"};

To push the object into an array called contacts:

contacts.push(contact);
Nebula
  • 6,614
  • 4
  • 20
  • 40
  • `push` will set the `0` property under the hood. Not sure why you think doing it directly isn't good practice. – Oriol Jan 23 '16 at 18:25
  • This is not true. Performance-wise, using the index is much faster than calling .push() for each item. – michael Jan 23 '16 at 18:26
  • Really, huh. That's funny, if it's really just an alias for `array[array.length] = ...`. – Nebula Jan 23 '16 at 18:26
  • In that case would it be better to do something like `var betterPush = (a, x) => a[a.length] = x`? – Nebula Jan 23 '16 at 18:27
  • We're talking about micro-optimizations at this point, but yes, you're adding the overhead of an additional function call every time you add an item. If you're adding a couple items, it really doesn't matter. If you're adding millions of items, you can start to see performance degradation when using .push(). – michael Jan 23 '16 at 18:28
  • If you know the current index, I'd recommend using a for-loop. If you don't know the length of the current array, some would argue using .push() despite the small hit in performance is worth the benefit you gain in readability. – michael Jan 23 '16 at 18:29
  • But even without a function call it looks like method 2 in this gist is slower.. https://gist.github.com/liam4/1e69797e4be7c0050da9 – Nebula Jan 23 '16 at 18:30
  • (In case one doesn't want to run the script, replacing the 1millions with 10millions and running makes method1 take 14768ms and method2 take 16132ms) – Nebula Jan 23 '16 at 18:32
  • @towerofnix "you should make an object, then push that object into the array." That would have been the way I would have done it, but this seems a very quick way, so I wondered about it. – Jason210 Jan 23 '16 at 18:36
0

Nothing wrong is here, or surprise. You get equivalent of next code

var contact = [{
  'name': "Peter Parker"
}];

At first you create array, and then put object in it.

zr9
  • 516
  • 1
  • 4
  • 13
0

There is nothing wrong with the code provided nor is it good or bad practice; that completely depends on the context (use the right tool for the right job concept).

All you're doing is creating an array where the first item is an object. You can nest arrays inside of arrays or objects inside of objects, or go even further and nest them multiple times.

michael
  • 748
  • 4
  • 10