Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    getting data from an array

    ok so im very new to js and im having trouble getting data from a 'multi dimensional array' i believe its called, here is a sample of my array:

    Code:
    catalogue=[ '3765834619:Neuro-linguistic Programming for Dummies:12.99:950',
    '5673423439:Excel 2007 VBA Programming for Dummies:11.99:900',
    '7454982674:The Forgotten Garden:9.99:450',
    '8356583865:The Road Home:9.99:500',
    '1629546824:No Time For Goodbye:10.99:600',
    '3547975624:The Outcast:10.99:670',
    '1348795463:How to Write Songs on Guitar:15.99:1010',
    '5673519472:Fretboard Roadmaps:12.99:700',
    '2730471465:Management and Organisational Behaviour:20.99:1000',
    '4859716360:Leadership Coaching:18.99:90',
    'ISBN:title:price:weight'];
    now im supposed to get certain details from the array and process them into an order but i don't know how to just get certain values out of the array (for example just getting the price and nothing else).

    thanks in advance
    gez
    Last edited by Gez; 08-08-2008 at 04:03 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Multi-dimensional arrays are usually made like this:

    Code:
    catalogue=[ new array('3765834619','Neuro-linguistic Programming for Dummies','12.99:950'); ]
    Then you access the items like this:

    Code:
    catalogue[0][0]
    The first zero selects the first entry in the catalogue array, and the second zero selects the first entry within that array.

  • Users who have thanked binaryWeapon for this post:

    Gez (08-08-2008)

  • #3
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ah thanks, that makes sence! but could you give an example of using two items from my stated array as im not sure how to, would you put the ; at the end of each line or mayb a , ?

    Gez
    Last edited by Gez; 08-08-2008 at 04:28 PM.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It is not a multidimensional array. It is a simple array with some strings as elements. Inside, we may say the strings have some inner delimiters (:). You may use the split() method to separate the string into parts delimited by the delimiter.

    But this is an inefficient way to structure data. You could have used from the beginning an object instead of an ordered array (or a combination object/arrays). Or you could have used a function constructor or a prototype constructor to populate it

    However, we may combine what you have with what you should have, and build now that object based on your array:
    Code:
    <script type="text/javascript">
    var catalogue=[
    '3765834619:Neuro-linguistic Programming for Dummies:12.99:950',
    '5673423439:Excel 2007 VBA Programming for Dummies:11.99:900',
    '7454982674:The Forgotten Garden:9.99:450',
    '8356583865:The Road Home:9.99:500',
    '1629546824:No Time For Goodbye:10.99:600',
    '3547975624:The Outcast:10.99:670',
    '1348795463:How to Write Songs on Guitar:15.99:1010',
    '5673519472:Fretboard Roadmaps:12.99:700',
    '2730471465:Management and Organisational Behaviour:20.99:1000',
    '4859716360:Leadership Coaching:18.99:90']
    ;
    var catSplit, j;
    var catObj={ISBN:[],title:[],price:[],weight:[]}
    for(var i=0;i<catalogue.length;i++){
    catSplit=catalogue[i].split(':'),j=0;
    	for(p in catObj){
    	catObj[p][catObj[p].length]=catSplit[j];
    	j++;
    	}
    }
    alert(catObj.ISBN);
    alert(catObj.title);
    alert(catObj.price);
    alert(catObj.weight);
    </script>
    Last edited by Kor; 08-08-2008 at 04:36 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Gez (08-09-2008)

  • #5
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks kor, my lecturer has told me to use the colon seperator format, but hes also said that we have to use 'simple' js that we have been givin in the lectur, i don't spose you of any simpler method or i guess im stuck emailing the lecturer?
    Gez

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Gez View Post
    Thanks kor, my lecturer has told me to use the colon seperator format
    But the colon is indeed the separator between property and value in an object:
    Code:
    var myObject={
    property1:value,
    property2:value,
    property2:value
    }
    So I suspect your lecturer was thinking of that kind of separator.

    Se also the objects/arrays notation in JSON (JavaScript Object Notation)
    www.json.org
    http://www.json.org/js.html
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    nope he actully gave me that format, but thanks for you help anyway.
    Gez

  • #8
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    -----
    Last edited by Cranford; 08-09-2008 at 08:09 PM.

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Gez View Post
    nope he actully gave me that format, but thanks for you help anyway.
    Gez
    Ok, but my code is based on that format, haven't you noticed? Which is the problem? I've offered you a solution. Any other solution will be more or less based on the same principle. What else are you looking for?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    sorry kor, i didn't mean to sound sarcastic or asking for more i am grateful and i have decided to use your method and hope i can get some marks for it, thanks.
    Gez


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •