/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Array Initialization problem

The following does not initialize correctly.
It errors on the first “mainArray[0] = new mainObj” indicating a missing ‘;’ in the statement.

What am I doing wrong?

[code]
<html>
<head>
<title> Array Association </title>
<script type=”text/javascript”>
var mainObj = { nameinfo : ”, hatsize : ”, necksize : ”, armlength : ”,
chest : ”, waist : ”, hips : ”, leglength : ”, shoesize : ” };
var mainArray = [];
mainArray[0] = new mainObj { nameinfo:’John’, hatsize:’6′, necksize:’14’, armlength:’28’,
chest:’42’, waist:’30’, hips:’34’, leglength:’28’, shoesize:’10’ };
mainArray[1] = new mainObj { nameinfo:’Jacob’, hatsize:’6.5′, necksize:’15’, armlength:’30’,
chest:’44’, waist:’32’, hips:’36’, leglength:’32’, shoesize:’11’ };
mainArray[2] = new mainObj { nameinfo:’Jingle’, hatsize:’7.5′, necksize:’16’, armlength:’32’,
chest:’42’, waist:’36’, hips:’38’, leglength:’36’, shoesize:’12’ };
mainArray[3] = new mainObj { nameinfo:’Heimer’, hatsize:’6.25′, necksize:’14.5′, armlength:’33’,
chest:’40’, waist:’33’, hips:’34’, leglength:’38’, shoesize:’11.5′ };
mainArray[4] = new mainObj { nameinfo:’Smith’, hatsize:’6.25′, necksize:’17’, armlength:’34’,
chest:’50’, waist:’44’, hips:’46’, leglength:’30’, shoesize:’10.5′ };
</script>
</head>
<body>
<script type=”text/javascript”>
var str = ‘<table border=”1″>’;
for (var i=0; i<mainArray.length; i++) {
str += ‘<tr>’;
str += ‘<td>’+mainArray[i].nameinfo+'</td>’
str += ‘<td>’+mainArray[i].shoesize+'</td>’
str += ‘</tr>’;
}
str += ‘</table>’;
document.write(str);
</script>
</body>
</html>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@mrhooAug 29.2009 — mainObj is not a constructor, and cannot initialize an instance.

If it was, you would need to include parenthesis around the new instance parameter.

You can assign to each array item an object-
[CODE]var mainArray= [];
mainArray[0]={
nameinfo: 'John', hatsize: '6', necksize: '14', armlength: '28',
chest: '42', waist: '30', hips: '34', leglength: '28', shoesize: '10'
};[/CODE]


//Or make a mainObj constructor function-

[CODE]
var mainArray= [];
function mainObj(obj){
for(var p in obj) this[p]= obj[p];
}

mainArray[0]= new mainObj({
nameinfo: 'John', hatsize: '6', necksize: '14', armlength: '28',
chest: '42', waist: '30', hips: '34', leglength: '28', shoesize: '10'
});[/CODE]
Copy linkTweet thisAlerts:
@scragarAug 29.2009 — The array is not your problem. It's you use of an object as a class(Which javascript doesn't really have, meaning you have to use a function as if it was a class).

[code=php]
function mainObj(atr){
for(var i in atr){
this[i] = atr[i];
}
}

var mainArray = [];
mainArray[0] = new mainObj({ nameinfo:'John', hatsize:'6', necksize:'14', armlength:'28',
chest:'42', waist:'30', hips:'34', leglength:'28', shoesize:'10' });

[/code]
Give that a run.
Copy linkTweet thisAlerts:
@JMRKERauthorAug 29.2009 — Thank you 'mrhoo' and 'scragar'.

The object creation technique works fine.

I have another question, but I'm going to play with what you have given me

to see if I can solve it myself.

Appreciate the insight!
Copy linkTweet thisAlerts:
@JMRKERauthorAug 29.2009 — Question:

In this test script, is this the best way to initialize the contents of the 'mainArray'?


I'm assuming the contents of 'ArrayInfo' could also be obtained from a text file, external JS or AJAX read function.

It appears to work fine in FF (have not experimented with MSIE yet)

but I'm still trying to get my head around objects vs. arrays and how to manipulate them. ?

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Array Association &lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1031767#post1031767
/* 'mrhoo' and 'scragar' suggestions */

var mainArray = [];
function mainObj(obj) { for (var p in obj) this[p] = obj[p]; }


/* information obtained from external text source */

var ArrayInfo = [ // name, hatsize, necksize, armlength, chest, waist, hips, leglength, shoesize
['John', '6', '14', '28', '42', '30', '34', '28', '10'],
['Jacob', '6.5', '15', '30', '44', '32', '36', '32', '11'],
['Jingle', '7.5', '16', '32', '42', '36', '38', '36', '12'],
['Heimer', '6.25', '14.5', '33', '40', '33', '34', '38', '11.5'],
['Smith', '6.25', '17', '34', '50', '44', '46', '30', '10.5']
];

function SetupRec(i) {
mainArray[i] = new mainObj({nameinfo:'',hatsize:'',necksize:'',armlength:'',
chest:'',waist:'',hips:'',leglength:'',shoesize:''});
mainArray[i].nameinfo = ArrayInfo[i][0];
mainArray[i].hatsize = ArrayInfo[i][1];
mainArray[i].necksize = ArrayInfo[i][2];
mainArray[i].armlength = ArrayInfo[i][3];
mainArray[i].chest = ArrayInfo[i][4];
mainArray[i].waist = ArrayInfo[i][5];
mainArray[i].hips = ArrayInfo[i][6];
mainArray[i].leglength = ArrayInfo[i][7];
mainArray[i].shoesize = ArrayInfo[i][8];
}

function mainArrayInitialization() {
for (var i=0; i&lt;ArrayInfo.length; i++) { SetupRec(i); }
}
onload = mainArrayInitialization();
/* */

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;
var str = '&lt;table border="1"&gt;';
str += '&lt;tr&gt;&lt;td colspan="9"&gt;Array size: '+mainArray.length+'&lt;/td&gt;&lt;/tr&gt;';
for (var i=0; i&lt;mainArray.length; i++) {
str += '&lt;tr&gt;';
str += '&lt;td&gt;'+mainArray[i].nameinfo+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].hatsize+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].necksize+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].armlength+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].chest+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].waist+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].hips+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].leglength+'&lt;/td&gt;' <br/>
str += '&lt;td&gt;'+mainArray[i].shoesize+'&lt;/td&gt;' <br/>
str += '&lt;/tr&gt;';
}
str += '&lt;/table&gt;';
document.write(str);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@mrhooAug 29.2009 — There is nothing at all wrong with your code,

but you [I]could [/I]create the new objects directly from their ordered arrays in the constructor.

[CODE]var mainArray= [];
function mainObj(obj){
if(obj.constructor== Array){
var n= 0, fieldnames= ['naminfo','hatsize','necksize','armlength','chest','waist','hips','leglength','shoesize'];
while(n < 9){
this[fieldnames[n]]= obj[n];
++n;
}
}
else{
for(var p in obj) this[p]= obj[p];
}
}



/* information obtained from external text source */
var ArrayInfo= [//name, hatsize, necksize, armlength, chest, waist, hips, leglength, shoesize
['John','6','14','28','42','30','34','28','10'],
['Jacob','6.5','15','30','44','32','36','32','11'],
['Jingle','7.5','16','32','42','36','38','36','12'],
['Heimer','6.25','14.5','33','40','33','34','38','11.5'],
['Smith','6.25','17','34','50','44','46','30','10.5']
];
function mainArrayInitialization(){
for(var i= 0; i<ArrayInfo.length; i++){
mainArray[i]= new mainObj(ArrayInfo[i]);
}

}[/CODE]


//test (with a toString method for reporting)

[CODE]mainObj.prototype.toString= function(){
var A= [];
for(var p in this){
if(this.hasOwnProperty(p)) A.push(p+'= '+this[p]);
}
return A.join(', ');
}[/CODE]

[B]mainArrayInitialization();

alert(mainArray.join('nn'));[/B]
Copy linkTweet thisAlerts:
@JMRKERauthorAug 29.2009 — Thank you!

?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...