|
| javaScript object as a parameter |
 |
Thu, 13 Mar 2008 11:18:04 -050 |
I having some trouble sending a javaScript object as a parameter to a
javaScript function. You should be able to run the sample code below my
signature in your html editor to view the results and hopefully give some
suggestions. I will put it in the dBase wrapper later.
The purpose is to create a general function into which different types and
lengths of data could be sent to create a table with column headers and
table data (that part I got working, I just don't want to have to hard code
this for each of my apps).
The function listProperties(obj, objName) Should be able to display the
properties of the jsData array. My problem is passing the "obj" and
"objName".
The result for the first element should look like this:
jsData[0].location = Uruguay
jsData[0].year = 1930
jsData[0].winner = Uruguay
jsData[0].winScore = 4
jsData[0].loser = Argentina
jsData[0].losScore = 2
But instead it look like this:
jsData.0=[object Object]
jsData.1=[object Object]
....
so I figure I must not be passing the parameter info correctly.
Claus
///////sample code/////////
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
// Table data -- an array of objects
var jsData = new Array();
jsData[0] =
{location:"Uruguay",
year:1930,
winner:"Uruguay",
winScore:4,
loser:"Argentina",
losScore:2};
jsData[1] =
{location:"Italy",
year:1934,
winner:"Italy",
winScore:2,
loser:"Czechoslovakia",
losScore:1};
jsData[2] =
{location:"France",
year:1938,
winner:"Italy",
winScore:4,
loser:"Hungary",
losScore:2};
jsData[3] =
{location:"Brazil",
year:1950,
winner:"Uruguay",
winScore:2,
loser:"Brazil",
losScore:1};
jsData[4] =
{location:"Switzerland",
year:1954,
winner:"West Germany",
winScore:3,
loser:"Hungary",
losScore:2};
jsData[5] =
{location:"Uruguay",
year:1930,
winner:"Uruguay",
winScore:4,
loser:"Argentina",
losScore:2};
function listProperties(obj, objName) {
var result = "";
for (var i in obj) {
result += objName + "." + i + "=" + obj[i] +
"\n";
}
alert(result);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<INPUT
TYPE="button"
value="List"
onClick="listProperties(jsData,'jsData')"
>
</BODY>
</HTML>
|
| Post Reply
|
| Re: javaScript object as a parameter |
 |
Thu, 13 Mar 2008 12:29:20 -050 |
Here is the explantion from the manual I am reading about "Doing Something
with a Property of an Object". Perhaps it will clarify what I am trying to
do.
Problem
You want to examine (or modify) the values of properties belonging to an
object, but the object and its properties may change from one examination to
another.
Solution
Use a for/in loop to access every property of an object, regardles of the
property's name. The following function assembles a list of properties and
their values for any object passed as an argument to the function
function listProperties(obj, objName) {
var result = "";
for (var i in obj) {
result += objName + "." + i + "=" + obj[i] +
"\n";
}
alert(result);
}
In this special type of loop, the variable (i in this example) is
automatically assigned the name of each property (in string form) as the
loop progresses throught the list of available properties for the object.
By using th string name as an index to the object (obj[i] in this example),
the value of that peroperty is returned.
Therefore in my sample data:
jsData[0].year = 1930
obj[i] = year
and objName = jsData[0]
in our language (dBase) this is an associtive array. But somehow I can't
figure out how to call the function with the arguments "obj" and
"objName"?
For you guru's Ken and Micheal this should be duck soup!
Claus
|
| Post Reply
|
| Re: javaScript object as a parameter |
 |
Thu, 13 Mar 2008 14:18:24 -050 |
Thanks Micheal,
what I wanted was a variable for "year"
> result += objName + "." + i + "=" + obj[i].year +
"\n";
According to my manual I should be able to capture that as it is just a
property of the object.
The idea being I could write one routine that would read a table. The
resulting rowset would include the name/value pairs. So the name/value
pairs could be a variable length depending on the table I was reading. From
it I would be able to extract both the name and the value via variables.
lets just say that a query resulted in the following information being
returned. I store that information in an array. From the array I can build
a table on my html page. For my table header I wanted to use the name
portion of the n/v pair.
var sales = new Array();
sales[sales.length] = {period:"q1", region:"east",
total:2300};
sales[sales.length] = {period:"q2", region:"east",
total:3105};
sales[sales.length] = {period:"q3", region:"east",
total:2909};
sales[sales.length] = {period:"q4", region:"east",
total:4800};
In simplestic terms would this data not look like this?
sales[0].period = q1
sales[0].region = east
sales[0].total = 2300
.......
sales[3].total = 4800
So using the object's index (sorry my reference is not very good here)
Should I not be able to capture the name of each of these
elements/properties or whatever they are?
I think they call this a simulated hash table. Anyway I want to capture
both "total" and "2300"
|
| Post Reply
|
| Re: javaScript object as a parameter |
 |
Thu, 13 Mar 2008 14:51:21 -040 |
Claus Mygind wrote:
>
> The result for the first element should look like this:
> jsData[0].location = Uruguay
> jsData[0].year = 1930
> jsData[0].winner = Uruguay
> jsData[0].winScore = 4
> jsData[0].loser = Argentina
> jsData[0].losScore = 2
>
> But instead it look like this:
> jsData.0=[object Object]
> jsData.1=[object Object]
Your for loop that reads the array: try it like this
result += objName + "." + i + "=" + obj[i].year +
"\n";
obj[i].year //or whatever "field" in the array you need.
|
| Post Reply
|
| Re: javaScript object as a parameter |
 |
Thu, 13 Mar 2008 16:30:34 -040 |
Claus Mygind wrote:
> Thanks Micheal,
>
> what I wanted was a variable for "year"
>
>> result += objName + "." + i + "=" + obj[i].year +
"\n";
>
> According to my manual I should be able to capture that as it is just a
> property of the object.
>
> The idea being I could write one routine that would read a table. The
> resulting rowset would include the name/value pairs. So the name/value
> pairs could be a variable length depending on the table I was reading.
From
> it I would be able to extract both the name and the value via variables.
>
> lets just say that a query resulted in the following information being
> returned. I store that information in an array. From the array I can
build
> a table on my html page. For my table header I wanted to use the name
> portion of the n/v pair.
>
> var sales = new Array();
> sales[sales.length] = {period:"q1", region:"east",
total:2300};
> sales[sales.length] = {period:"q2", region:"east",
total:3105};
> sales[sales.length] = {period:"q3", region:"east",
total:2909};
> sales[sales.length] = {period:"q4", region:"east",
total:4800};
>
>
> In simplestic terms would this data not look like this?
>
> sales[0].period = q1
> sales[0].region = east
> sales[0].total = 2300
> .......
> sales[3].total = 4800
>
> So using the object's index (sorry my reference is not very good here)
> Should I not be able to capture the name of each of these
> elements/properties or whatever they are?
Each element in the array stores an object.
sales[1] is the second element in the array "sales"
And stored in this element is the object:
{period:"q2", region:"east", total:3105};
so, we can:
var o = sales[1]
o.period = q2
etc.
There may be way to traverse the object elements, but I've not found it.
The following is a modification of the the web form you posted. I've added a
second push button and changed the function.
function listProperties(obj, objName) {
var result = "";
for (var i in obj) {
var object = obj[i]
var value = object[objName]
result += objName + "." + i + "=" + value + "\n";
}
alert(result);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<INPUT
TYPE="button"
value="List year"
onClick="listProperties(jsData,'year')"
>
<INPUT
TYPE="button"
value="List location"
onClick="listProperties(jsData,'location')"
>
|
| Post Reply
|
|
|