Skip to content
Advertisement

How does one create an object from an array of data items?

I’m struggling to create an object from an array in JS. I keep getting an error when pushing into the plots object.

JavaScript

Advertisement

Answer

  1. In JS, an array has no named keys, it’s only a list of things. If you want named keys, use an object {}
  2. plots[status] is never initialized. When you try to .push() stuff in something undefined, the script crashes. Initialize it to an empty array before starting to push things in it.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement