• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding an object with an array, as a value of another object

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I’m playing around with JavaScript objects. I can make a simple object no problem, what I’m having trouble with is nesting an object that contains arrays, as a property of another object. Here’s a sample to show what I’m trying to do:




When it gets to the point where I'm inserting data into the array, it's telling me "Unable to get value of the property 'colors': object is null or undefined". I've tried adding this:



but that didn't help either. Anybody know how to accomplish this?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me see if I can figure that out.

In lines 4-8 you create a variable named "car" and assign it three attributes. The first two attributes ("make" and "model") get assigned String values, and the third ("arr") gets assigned something which is undefined.

Then in lines 10-12 you create a variable named "arrays" which contains only one attribute, an empty array. By coincidence that variable has the same name as the thing which was undefined at line 7.

Subsequently you never use the "arrays" variable, but you do attempt to use the "arr" attribute of the "car" variable, which as we already know was given an undefined value.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of arr in the first place? Don't make things more complicated than they need to be.

How I'd do what I think you are trying to do:


By the way, my 1965 Mustang was maroon.
 
J Miller
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the idea is that I'm trying to organize my variables a little better. Currently I have a massive blob of variables at the top. What I’d like to do is group them, perhaps by type. So, for example, I was thinking of having an object named “global” that would have some generic properties (make, model, etc). Then another object with several arrays (color, tire size, etc) as its properties. Then having that object be a property of the global object. So that I’d be able to do something like:

global.colors = [“red”, “maroon”];

So in the example above, I’m trying to have a type named “arrays” that contains arrays. Then I ‘m trying to make a property in the “car” object, named “arr”, that is of the type “arrays”. Eventually the “arrays” object might contain several arrays that I can interact with by saying “car.arr.<arrayName>”
Hope this makes sense.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That all strikes me as just kinda weird. Firstly, just as with any other language, globals need to be minimized or avoided, so formalizing them in this way actually seems counter to any proper approach.

Closures help to keep variables out of the global scope. I suggest some reading up on them.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code does not work because arrays would have to be defined before car. I agree that that approach is weird.

I would expect something like



or



The first way is better because if another developer comes along, they can see the data structure without having to search code to figure out where colors is added.


Eric
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic