09 08 08 179 W - + 8 - 8 Javascript array annoyance from database results
A common thing done in webdevelopment is using data from a database.
In your average PHP code you'd just do a while( list( ,$val ) = each( $dbResult ) ) loop, maintaining the correct order for the query results.
Now for the twist!
If you want to quite literally use your query result, an array of rows with their associated fields and values, in Javascript you can't really convert that one-to-one.
So, what's the solution?
Simple!
You make an array and a function to add the rows to your result!
Unfortunately, you can not just add it automatically to the array, so you need some kind of loop.
Here's an example:
[quote]
function newRow ( id, value )
{
this.id = id;
this.value = value;
}
var myTable = new Array();
myTable[0] = new newRow( 1, "User1" );
myTable[1] = new newRow( 2, "User2" );
[/quote]
You can generate that list quite easily in any programming language and/or templating engine and you can use it to feed it to all of your functions as well.
Because it's based on objects, you get to use entry.name too!