6
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

4 Answers 4

10

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" }];
Sign up to request clarification or add additional context in comments.

Comments

1

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(', '));
}​

Comments

1

You could create an array like:

var selectColumns = [
    { TableName: "DeploymentRosterView", ColumnName: "ArrivedOn" },
    { TableName: "DeploymentRosterView", ColumnName: "DepartedOn" },
    { TableName: "DeploymentRosterView", ColumnName: "RoleType" },
    { TableName: "DeploymentRosterView", ColumnName: "AdjusterFirstName" },
    { TableName: "DeploymentRosterView", ColumnName: "AdjusterLastName" }
];

8 Comments

Please note - this is not JSON. That is an array literal with object literals. JSON has very specific syntax, which this is not. See ietf.org/rfc/rfc4627.txt?number=4627
rule of thumb - if it is a string its JSON if it is a JS object then its just a JS object
@missingno - I've always kinda understood that there are no "json objects", but your comment really solidified it for me...thank you, and +1 on your last answer :)
@Chris you know, the syntax is the same, instead of var selectColumns = and quotes for keys.
Two downvotes is not a "terrible reaction". The answer is technically incorrect, that's what the downvote mechanism is for. You were given a reason for the downvotes, and you could have taken that chance to correct the inaccuracy (I would have removed my down vote had you done so), instead you're defending the inaccuracy with a shrug and a "it's close enough" (which is a further inaccuracy)? Too bad.
|
0

You can also use some tricks to avoid retyping some things in this particular case

var columnNames = ["ArrivedOn", "DepartedOn", "RoleType", "AdjusterFirstName", "AdjusterLastName" ];

var selectColumns = columnNames.map(function(colName){
    return { TableName: "DeploymentRosterView", ColumnName: colname };
});

Do note that map is not available by default on old browsers (but variations of it are very popular in libraries and shims)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.