...

View Full Version : getting data from an array



Gez
08-08-2008, 02:58 PM
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:


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

binaryWeapon
08-08-2008, 03:21 PM
Multi-dimensional arrays are usually made like this:


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


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.

Gez
08-08-2008, 03:24 PM
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

Kor
08-08-2008, 03:31 PM
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:


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

Gez
08-08-2008, 03:38 PM
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

Kor
08-08-2008, 04:05 PM
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:


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

Gez
08-08-2008, 04:15 PM
nope he actully gave me that format, but thanks for you help anyway.
Gez

Cranford
08-08-2008, 06:38 PM
-----

Kor
08-08-2008, 08:24 PM
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?

Gez
08-09-2008, 04:03 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum