Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why is my object undefined? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I have the following code
Code:
<html>
<head>
<script>
var records = new Array();

function setPage1Records(tp)
{
	html = "col1|val1||col2|val2|||Col1|val3||col2|val4";

	var recs = html.split("|||");
	
	for (var i=0; i<recs.length; i++)
	{
		records[i] = new Object();
		records[i][tp] = new Object();
		var data = recs[i].split("||");
		for (var j=0; j<data.length; j++)
		{
			var vals = data[j].split("|");
			records[i][tp][vals[0]] = vals[1];
		}
	}

	alert(records[1].summary.col1);
}
</script>
</head>
<body>
<input type="button" value="testing JS Object" onclick="setPage1Records('summary');" />
</body>
</html>

why do I not have the records array equaling the following data structure...

records[0].summary.col1 = 'val1'
records[0].summary.col2 = 'val2'
records[1].summary.col1 = 'val3'
records[1].summary.col2 = 'val4'

the alert is showing undefined?

Cheers,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
>html = "col1|val1||col2|val2|||Col1|val3||col2|val4";
[tt]html = "col1|val1||col2|val2|||[highlight]c[/highlight]ol1|val3||col2|val4";[/tt]
 
Wow , never noticed the typo good spot!

Well it was late last night, however, even when I tried

records[1].summary.col2

It showed undefined also, I take it my syntax and logic is sound?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
It then works for me after the correction, for records[1].summary.col1 you chose to alert. The rest, it works from the beginning.
 
Cool,

Handling complex records sets can do your head in when burning the midnight oil.

Perhaps my blurry eyes were playing tricks on me.

Thanks for your help.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I was right i did have the wrong syntax, once I wrote the logic into the real app all the screen fields got updated with 'undefined',

If I used
Code:
pageHTML = "Name|Paul Jones|vp|Job|Programmer|vp|Phone|0800 800 800|nl|Name|John Smith|vp|Job|Analyst|vp|Phone|0900 900 900";                                                         
var tp = 'summary';
        var recs = pageHTML.split("|nl|");

        for (var i=0; i<recs.length; i++)
        {
            if(tp == 'summary')
            {
                records[i] = new Object();  
            }
        
            records[i][tp] = new Object();
        
            var data = recs[i].split("|vp|");
        
            for (var j=0; j<data.length; j++)
            {
                var vals = data[j].split("|");
                records[i][tp][vals[0]] = vals[1];
            }
        }

later on I was using...
Code:
    for (var key in records[recID].summary)
    {         
        document.getElementById(key).innerHTML = rec.key;           
    }

I finally realised I was refering to the object wrong using the dot notation instead of square bracket.

It should be...
Code:
document.getElementById(key).innerHTML = rec[key];

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Just a supplementary note:
If you take the variable and feed to dot-notation, it is not going to come out right (or as said 'undefined'). In that case, as an illustration, it can be output like this.
[tt]
switch (key) {
case 'Name':
document.getElementById(key).innerHTML = records[recID].summary.Name;
break;
case 'Job':
document.getElementById(key).innerHTML = records[recID].summary.Job;
break;
case 'Phone':
document.getElementById(key).innerHTML = records[recID].summary.Phone;
break;
default:
break;
}
[/tt]
Sure it is not attractive as an alternative. It is just to demonstrate the proper use of it, just like in the original col1, col2,..., the alert() would always show up correct value as col1, col2 are the literal value of the key.

There is not much of a difference in the use document.forms[], document.forms[].elements[] or window[] in other context. Just have to have a correct reasoning pattern. (Such as in the case of col1, Name etc, if they actually have some spaces in them, the dot-notation has to be avoided as they would not fit in the (naming) rules they are allowed to use.)
 
Sorry Tsuji, you've lost me a bit.

quote]If you take the variable and feed to dot-notation[/quote] I'm not familar with this expression. Do you mean not use a variable to hold variable name but use a literal?

Which I think you imply by this
as col1, col2 are the literal value of the key.

Lke you say your example is ugly not to mention long winded and non-dynamic.

i will have many pages all with random ID's on them and I want the one sub to process all pages, so having to know the names of the ID's, I don't think on this occasion is an option.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
My example of alternative renderment, I would not say urly. I would say it is useful for illustration of the point. (People who is used to think unidimensional can have all sort of judgement shaky. That kind of looks can easily happen in functional programming or compiler writing.)

To put thing more easily comprehensible, these lines.
[tt]
key="Name"
records[recID].summary[key] //correct return
records[recID].summary["Name"] //correct return
records[recID].summary.Name //correct return
records[recID].summary.key //return undefined
records[recID]["summary"]["Name"] //correct return
records[recID]["summary"].Name //correct return
records[recID]["summary"][key] //correct return
records[recID]["summary"].key //return undefined
[/tt]
There is nothing to say in the original setting with col1, col2... What said is proper; cannot say syntax error there.
 
Sure it is not attractive[/good] what's the oposite to attractive ;-), in Cinderalla they are not called the three unattractive sisters [lol]

But seriously, I guess you're saying the only way to get variable interpolation is by using square bracket notation.

Again thanks for your help.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
>[self][tt]Sure it is not attractive as an alternative[/tt]
Unfortunately, that is not the same as:
[tt]Sure it is urgly (period).[/tt]
At the end, who care personal opinion?!
 
come on tsuji it was a joke, it was merley a play on words, sorry was it too early on a Monday morning for sarcasm?

However, I will still disagree, IMO ugly = unattractive , however beauty is in the eye of the beholder!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I understand, 1DMF. Have no cause to worry the contrary, just fear the opinion interferes with your taking up of the syntax. It does, for some.
 
just fear the opinion interferes with your taking up of the syntax

You mean using hardcoded dot notation syntax is preferable than dynamic with the square bracket notation?

Why is that? what's the benefit of using long winded hardcoded dot natation vs dynamic square bracket notation?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I do not preach. I really am happy with both.
 
I'm really confused, you say my opinion over being ugly may interfere with taking up a syntax.

But if it doesn't matter which syntax is used how is it interfering with anything?

If I'm doing something wrong or bad I really would like to know so I do it right.

Last thing I need is to spend months developing an application if the code is flawed.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
There are three sets of script blocks being visited. As long as each being applied within their domain of validity, the first two sets are very similar and the third is more for massaging javascript syntax to deepen your understanding. You have selected a valid syntax out of the two, and that suffices. If you look for other persons script, knowing alternatives may help. If you take one valid syntax and take it as the only valid way to do thing, that is very comforting and is fine for me as well. So I don't any cause of alarm. You have made a good choice.
 
Phew, thanks for your help tsuji, it's my first attempt at a full ajax application with data sets, so am glad I'm on the right track.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top