Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Complex Javascript variable example using colons for hashes. Nested hashes, arrays,etc

Posted on 2005-04-10
8
Medium Priority
?
230 Views
Last Modified: 2008-02-01
Could someone give me an example of a complex js variable using ':' for hash declarations? I recall hearing this could be done. Here's an example in perl for what I'd want in JS.

$me = {
     'cars' => [
            {'make' => 'ford',
              'model' => 'Mustang},
            {'make' => 'Ford',
              'model' => 'Escourt'}
    'computers'
....

Thanks,
Zumpoof
0
Comment
Question by:zumpoof
  • 3
  • 3
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13746412
I use a function

function car(model,make) {
  this.model=model
  this.make=make
}
cars = new Array(
  new car("ford","mustang"),  
  new car("ford","escort")
)

But you are right and I cannot remember the syntax which does resemble the perl one
0
 
LVL 8

Accepted Solution

by:
anthonywjones66 earned 200 total points
ID: 13746580
How about this:-

var me = {cars: [
            {make: 'ford', model: 'Mustang'},
            {make: 'Ford', model: 'Escourt'} ],
    computers: [{make: 'dell', model: 'dimension'} ]}

I don't know php at all but I'm guessing you wan me to have two properties 'cars' and 'computers'  which are arrays of objects that have the properties 'make' and 'model'.

Anthony.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13746595
Thanks Anthony. I was looking for that too yesterday

me = {
  cars: [
    {make: 'ford', model: 'Mustang'},
    {make: 'Ford', model: 'Escort'}
  ],
  computers: [
    {make: 'dell', model: 'dimension'}
  ]
}

document.write("me")
for (o in me)
  for (p in me[o]) document.write('<br>'+ o+':'+me[o][p].make+'/'+me[o][p].model)

Points to Anthony
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:SnowFlake
ID: 13746712

There is a site that Is pushing for that use of this syntax
(JavaScript Object Notation)
http://json.org
In other languages as well.

0
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13746847

I've considered JSON in past but as yet I haven't found a way to use it to populate an object that has both data and function.  It would be nice to have a set of prefined 'classes' (functions with extended prototypes) that can be instanced and populated with JSON or JSON like syntax.  As yet I've not been able to solve that one.  It is a very appealling alternative to XML.

Anthony.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13746884
What are you missing?
Seems all is missing is a toString

http://www.crockford.com/JSON/js.html
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13746891
anthony:
function mult(){
return this.data1*this.data2
}

var thisobject = { data1:1,
                          data2:2,
                          f1:function(){return this.data1+this.data2} ,
                          f2:mult
}

alert(thisobject.f1());
alert(thisobject.f2());
thisobject.data1=3;
alert(thisobject.f1());
alert(thisobject.f2());
0
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13746938
Chaps,

I don't want to take up too much of your time or hijack this question for one of my own.  When I get the chance I'll formulate description of what I would've liked to do with JSON as a base then perhaps one of you clever boffins might see a solution and earn some points in the process. :)

Anthony.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question