I have written some code where in a loop, a function is called which passes the current object in that loop iteration to the function.
The problem is I don't know how to use the object because its dynamic and I can only access the obj parameter.
Javascript
objectifyTableRow(val, i); // Populate object with properties and values
Val is an object which is passed to function objectifyTableRow
function objectifyTableRow(objRow, count) { // Create objects for calculations
var ii = 0;
$('#ctl00_PageContent_freight_rate_column_chaair_r' + count + " " + 'td').each(function(i, val) { // begin each
/* Concatenate column name with subcolumn name. example objFobCost.China/Sea Origin,Air */
if (i < 3) { // columns 0,1,2 in table row
if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
var PropertyName = arrayColumns[0] + arraySubColumn[ii];
objRow[PropertyName] = parseFloat($(val).html()); // Set key name with var PropertyName
ii += 1;
}
if (ii == 3) { // Reset counter
ii = 0;
}
} // end of outer if
else if (i > 2 & i < 6) {
if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
var PropertyName = arrayColumns[1] + arraySubColumn[ii];
objRow[PropertyName] = parseFloat($(val).html());
ii += 1;
}
if (ii == 3) { // Reset counter
ii = 0;
}
} // end of outer if
else if (i > 5 & i < 9) {
if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
var PropertyName = arrayColumns[2] + arraySubColumn[ii];
objRow[PropertyName] = parseFloat($(val).html());
ii += 1;
}
if (ii == 3) { // Reset counter
ii = 0;
}
} // end of outer if
else if (i > 8 & i < 12) {
if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
var PropertyName = arrayColumns[3] + arraySubColumn[ii];
ii += 1;
}
if (ii == 3) { // Reset counter
ii = 0;
}
} // end of outer if
else {
if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
var PropertyName = arrayColumns[4] + arraySubColumn[ii];
ii += 1;
}
if (ii == 3) { // Reset counter
ii = 0;
}
} // end of else
}); // end of each loop TD
beginCalc(objRow);
};
Each object is passed to beginCalc so calculation can be made based on ID, Key and value
function beginCalc(obj) {
// Every obj used is passed to here
$.each(obj, function(key, element) {
alert('ID: ' + obj[this.id] + '\n' + 'key: ' + key + '\n' + 'value: ' + element); // Check correct obj id, key and value
});
The reason I am doing this is because the objects are storing values from an asp.net grid and I thought it would be cleaner to create objects for each grid row and then do the calculations in the code by selecing obj.key: value * obj.key: value. Rather than using getDocumentByElementId.
Any ideas how to programmatically access these objects by their ID in beginCalc function?
Thanks
beginCalc()
? In that case you'll need to create that full array first, save it as a variable, and then iterate over that... there's no way to magically get a list of objects that hasn't been created yet. Maybe I'm still misunderstanding. – Cecchi Jul 5 '12 at 13:53