Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Array Syntax

var selectColumns = new Array();
selectColumns[0] = { TableName: "DeploymentRosterView", ColumnName: "ArrivedOn" };
selectColumns[1] = { TableName: "DeploymentRosterView", ColumnName: "DepartedOn" };
selectColumns[2] = { TableName: "DeploymentRosterView", ColumnName: "RoleType" };
selectColumns[3] = { TableName: "DeploymentRosterView", ColumnName: "AdjusterFirstName" };
selectColumns[4] = { TableName: "DeploymentRosterView", ColumnName: "AdjusterLastName" };

The above works to create my array in JavaScript, but is there a more graceful way to write the above? Like an Object Initializer? Just wondering

like image 730
Mark Avatar asked Nov 30 '11 15:11

Mark


2 Answers

You can create it inline:

var selectColumns = [
    { TableName: "DeploymentRosterView", ColumnName: "ArrivedOn" },
    { TableName: "DeploymentRosterView", ColumnName: "DepartedOn" },
    { TableName: "DeploymentRosterView", ColumnName: "RoleType" },
    { TableName: "DeploymentRosterView", ColumnName: "AdjusterFirstName" },
    { TableName: "DeploymentRosterView", ColumnName: "AdjusterLastName" }];
like image 62
Adam Rackis Avatar answered Sep 30 '22 08:09

Adam Rackis


You could restructure it like so:

var selectColumns = {

  "DeploymentRosterView": {
    "ColumnNames": ["DepartedOn", "RoleType", "AdjusterFirstName", "AdjusterLastName"]
  }
};

// Loop through all views
for (view in selectColumns)
{
  var v = selectColumns[view].ColumnNames;
  alert('Column names for "' + view + '" is: ' + v.join(', '));
}​
like image 43
Gary Green Avatar answered Sep 30 '22 07:09

Gary Green