• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Array to Object Array

Hi,
In the following code I need to convert the array into object array and make it work.
Example:

var menu=new Array('What You Like ????','Hot Drinks','Cold Drinks','Nothing');
    var menu1=new Array('Oh Hot Drinks Then what u want ??','Coffee','Tea');
    var menu11=new Array('Good You want Coffee. Go ahead and select next','Espresso','cappuccino');
    var menu12=new Array('You want Tea??','GreenTea','WhiteTea');
    var menu2 = new Array('You have chosen cold drinks, make your choice','Milkshake','Soda');
    var menu21 = new Array('You want Milkshake','Strawberry','Mango');
    var menu22 = new Array('You have chosen Soda', 'Pepsi','Sprite');
      var menu3 = new Array('Nothin','Sample','Sample1');
      var menu31 = new Array('x','y','z');
      var menu32 = new Array('a','b','c');

I need help in change this as

var data = new Object();
data['start']=['What You Like ????','Hot Drinks','Cold Drinks','Nothing'];
data['Hot Drinks'] = .

and make the following code to work. Wondering how to do it.. Can somebody help me with it

Check with the attached file
dynamic.html
0
sana2009fall
Asked:
sana2009fall
1 Solution
 
Bryan ButlerCommented:
Sounds like you want an associate array using an enumerated index.  This can be done in programming languages.

Does either of these pages look like what you're after?

http://www.developertutorials.com/tutorials/javascript/enumerating-javascript-objects-060822/page1.html
http://www.ejball.com/EdAtWork/CategoryView,category,JavaScript.aspx
(there's an enumerate arrays section)
0
 
Bryan ButlerCommented:
That should have been "associative array"
0
 
sana2009fallAuthor Commented:
I was wondering how do I change it to reflect the object array. In my code it is got menu11,menu12,.. This has to be changed to reflect data['x'] and so on. Please help me with this
0
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!

 
Shahzad Fateh AliWeb Solutions Architect - VentureDive (Pvt) LtdCommented:
Hi,


Here is a little code written in Java Script which will work for you.
Remember that Java Script treats every variable as Object.
Regards,


Shahzad Fateh Ali

<script type="text/javascript">
var data = new Array();

data['start']=['What You Like ????','Hot Drinks','Cold Drinks','Nothing'];
data['Hot Drinks'] = {type:'Coffee', brand:'Nescafe'};

var hd = data['Hot Drinks'];

alert(data['Hot Drinks'].type);
alert(hd.brand);
alert(data[data['start'][1]].type);
</script>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
That is a bit messy

Try JSON

Here is an example. I do not personally like the embedded html, but it shows the idea

http://eisabainyo.net/weblog/2009/07/17/dropdown-menu-population-using-json-and-jquery/
0
 
Michel PlungjanIT ExpertCommented:
For example



var data = {'What You Like ????':"",
  'Hot Drinks':{
    'Oh Hot Drinks Then what u want ??':"",
    'Coffee': ['Good You want Coffee. Go ahead and select next','Espresso','cappuccino'],
    'Tea':['You want Tea??','GreenTea','WhiteTea']
  },
  'Cold Drinks': {
    'You have chosen cold drinks, make your choice':"",
    'Milkshake':['You want Milkshake','Strawberry','Mango'],
    'Soda':['You have chosen Soda', 'Pepsi','Sprite']
  },
  'Nothing':['Nothin','Sample','Sample1']
}

//alert(menu['Cold Drinks']['Milkshake'][1])


for (var o in data) alert(o+':'+data)
for (var p in data[o]) alert(p+':'+data[o][p]); // for nested object arrays
for (var i=0;i<data[o].length;i++) alert(i+':'+data[o][i]); // for nested arrays

Open in new window

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now