Groups > dBase > dBase Internet > Re: Creating a Custom Object




Creating a Custom Object

Creating a Custom Object
Mon, 31 Mar 2008 13:09:51 -050
I have been working along the lines of custom objects instead of arrays in 
javascript.

My problem is this, I want to substitute a variable for a constant so I can 
loop through data returned from an Ajax call.

The following short cut constructor will create the custom object with its 
properties:
var employeeDB = [{name:"Alice", age:23},
                  {name:"Fred", age:32},
                  {name:"Jean", age:28},
                  {name:"Steve", age:24}];


I have a value returned from the Ajax call that looks something like this:

name:"Alice", age:23;name:"Fred",
age:32;name:"Jean", age:28;name:"Steve", 
age:24

I just don't seem to be able to build a string where each of the above are 
represented by a variable.  It is important to note that "name" and
"age" 
will change from query to query as will the number of name/value pairs.

Claus 

Post Reply
Re: Creating a Custom Object
Mon, 31 Mar 2008 13:18:17 -050
A quick test of the following returned the wrong information

> var employeeDB = [{name:"Alice", age:23},
>                  {name:"Fred", age:32},
>                  {name:"Jean", age:28},
>                  {name:"Steve", age:24}];

if you run it in a script as shown below you will get 0,1,2,3 as the key 
value

 var object = employeeDB[0];

 //loop through each property (field list) of first record (name/value pair) 
returned
 for (var key in object)
 {
  alert(key);
 }


if you run it again and remove the square brackets [ ] then the result is 
correct ie: "name" and "age"

 var employeeDB = {name:"Alice", age:23},
                  {name:"Fred", age:32},
                  {name:"Jean", age:28},
                  {name:"Steve", age:24}; 

Post Reply
about | contact