File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
J Miller
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Pie
Posts: 20208
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic