aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes Adding an object with an array, as a value of another object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Adding an object with an array, as a value of another object" Watch "Adding an object with an array, as a value of another object" New topic
Author

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

J Miller
Ranch Hand

Joined: Oct 21, 2010
Posts: 62
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56229
    
  13

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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
J Miller
Ranch Hand

Joined: Oct 21, 2010
Posts: 62
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56229
    
  13

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.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15357
    
    6
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Adding an object with an array, as a value of another object
 
Similar Threads
Passing Argument To JavaScript Method
DOJO1.2.3/ IE7 /addOnLoad()
jQuery selector using array of objects
Chaining JavaScript Functions
How to pass Object to Javascript function in JSP