Array to Object Array

Posted on 2010-01-06
Last Modified: 2012-05-08
In the following code I need to convert the array into object array and make it work.

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
Question by:sana2009fall
    LVL 16

    Expert Comment

    by:Bryan Butler
    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?,category,JavaScript.aspx
    (there's an enumerate arrays section)
    LVL 16

    Expert Comment

    by:Bryan Butler
    That should have been "associative array"

    Author Comment

    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
    LVL 9

    Expert Comment

    by:Shahzad Fateh Ali

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

    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);

    Open in new window

    LVL 75

    Expert Comment

    by:Michel Plungjan
    That is a bit messy

    Try JSON

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

    Accepted Solution

    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']
    //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


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now