...

View Full Version : Resolved Need help regarding data retrieval from Javascript Array



maxkohbb
12-22-2010, 03:07 AM
Need help regarding data retrieval from Javascript Array

Hi,

I am new to Javascript and I have some difficulties regarding data retrieval on multi-dimensional array using Javascript.

This is my list of array:




var myArray= new Array(4);


myArray[0]= new Array(7);
myArray[0][0] = "6";
myArray[0][1] = "Billy";
myArray[0][2] = "Shipping";
myArray[0][3] = "great idea never sleeps";
myArray[0][4] = "41";
myArray[0][5] = "actual_264.jpg";
myArray[0][6] = "thumbnail_364.jpg";


myArray[1]= new Array(7);
myArray[1][0] = "1";
myArray[1][1] = "John";
myArray[1][2] = "Mail Order";
myArray[1][3] = "Awesome shopper.";
myArray[1][4] = "22";
myArray[1][5] = "actual_596.jpg";
myArray[1][6] = "thumbnail_696.jpg";


myArray[2]= new Array(7);
myArray[2][0] = "2";
myArray[2][1] = "Daniel";
myArray[2][2] = "Cash Payment";
myArray[2][3] = "Love to visit blogshops";
myArray[2][4] = "24";
myArray[2][5] = "actual_138.jpg";
myArray[2][6] = "thumbnail_238.jpg";


myArray[3]= new Array(7);
myArray[3][0] = "3";
myArray[3][1] = "Mary";
myArray[3][2] = "Shipping";
myArray[3][3] = "Find within oneself";
myArray[3][4] = "52";
myArray[3][5] = "actual_298.jpg";
myArray[3][6] = "thumbnail_398.jpg";



Is there a way I could sort the data being retrieved back such as, I wanted to loop the information such as



for(var i=0; i < myArray.length; i++){
//where “Shipping” is a variable by user
If (myArray[i][2] == "Shipping") {
//how could I write it to display all results with the users who have shipping
}


If I were to write it in a SQL statement, it would be something like,
SELECT * FROM myDatabase WHERE myArray[i][2] = 'Shipping'

Thanks.

Old Pedant
12-22-2010, 03:43 AM
You're going to love how easy this is:


for(var i=0; i < myArray.length; i++)
{
if (myArray[i][2] == "Shipping") /* if MUST be all lower case */
{
document.write( myArray[i] + "<hr>" );
}
}

By default, when you ask to convert an array to a string, JS outputs all the elements separated by commas.

If you want it fancier than that, you could (for example) do:


for(var i=0; i < myArray.length; i++)
{
if (myArray[i][2] == "Shipping") /* if MUST be all lower case */
{
document.write( myArray[i].join("<br>") + "<hr>" );
}
}

Or variations on that.

CAUTION: You can't use document.write once a page has been loaded!! I hope you knew that. You'd have to dump the data into, say, the innerHTML of some <div>.

Or you could do all sorts of things. Such as create a <table> with one <tr> row per matched record. Or or or or...

maxkohbb
12-23-2010, 12:10 PM
Works perfectly!
Thank you so much! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum