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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doubts over basic JS syntax 1

Status
Not open for further replies.

Sleidia

Technical User
Joined
May 4, 2001
Messages
1,284
Location
FR

I guess I've been too much into PHP during the past years because I can't remember why the following Javascript syntax is faulty :

var some_array = new Array();

some_array[0][] = 'a0';
some_array[0][] = 'a1';
some_array[0][] = 'a2';

some_array[1][] = 'b0';
some_array[1][] = 'b1';
some_array[1][] = 'b2';

Thanks ! :)
 
you need to define an index for your second level...

Code:
some_array[0][0] = 'a0';
some_array[0][1] = 'a1';
some_array[0][2] = 'a2';

some_array[1][0] = 'b0';
some_array[1][1] = 'b1';
some_array[1][2] = 'b2';



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
this could be the blind leading the blind here but i dont think that js imputes the index like php does.

so the empty [] don't work. use the count of the array less 1 technique or an incremental.

 
What I'm trying to do is building a Javascript array during the output of the HTML page as seen below. But the problem is that I get the following errors :

table_row_history[1] has no properties
table_row_history[2] has no properties
table_row_history[3] has no properties

Code:
<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!-- 

var table_row_history = new Array();

// End -->
</SCRIPT> 

( ... some html here ... )

<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!-- 

table_row_history[1][0] = 'some string';
table_row_history[1][1] = 'some string';
table_row_history[1][2] = 'some string';

// End -->
</SCRIPT>

( ... some html here ... )

<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!-- 

table_row_history[2][0] = 'some string';
table_row_history[2][1] = 'some string';
table_row_history[2][2] = 'some string';

// End -->
</SCRIPT>

( ... some html here ... )

<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!-- 

table_row_history[3][0] = 'some string';
table_row_history[3][1] = 'some string';
table_row_history[3][2] = 'some string';

// End -->
</SCRIPT>
 
As illustration of different methods.
[tt]
<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!--
var table_row_history = new Array();
// End -->
</SCRIPT>
( ... some html here ... )
<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!--
[red]table_row_history[1]=new Array();[/red]
table_row_history[1][0] = 'some string';
table_row_history[1][1] = 'some string';
table_row_history[1][2] = 'some string';
// End -->
</SCRIPT>
( ... some html here ... )
<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!--
[blue]table_row_history[2]=new Array('some string 0', 'some string 1', 'some string 2');[/blue]
// End -->
</SCRIPT>
( ... some html here ... )
<SCRIPT LANGUAGE=\"JavaScript1.2\">
<!--
[blue]table_row_history[3]=['some string 00', 'some string 11', 'some string 22'];[/blue]
// End -->
</SCRIPT>
[/tt]
 
Hi again Tsuji :)

May I ask one more question?

If we have this :

Code:
var my_array = new Array();

function f1() {

my_array[1] = new Array();
my_array[1][my_array[1].length] = 'some string';
my_array[1][my_array[1].length] = 'some string';
my_array[1][my_array[1].length] = 'some string';

}

function f2() {

alert(my_array[1].length);

}

f1();
f2();  // showing zero in the alert box!

So, how do I move my_array[1] on the same scope as my_array so that it shows 3 instead of zero?

Thanks for the help :)
 
Sleidia, it should show 3 already, should it not? my_array[1] is not restricted in scope. Is there something else being overlooked?
 

Hi Tsuji !

You're right, there was a problem with my code :(

I've fixed it and, at the same time, I've found that you can access restricted variables by using window.varname, which I didn't know.

Thanks for always being here anyway :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top