1

I would like to use dot notation in an object literal name but IE returns an error: "Expected ':'" at line 16 after the partial 'my' statement (3rd from the bottom). There has to be a way to do something like this. Why is this returning an error?

<script language="javascript">
var my = {};
my.dataGridColumns = [];
var tmpArr = [];    
var columnData = [];

columnData.push("a");

my.dataGridColumns.push({
    id: 1,
    name: test
});

tmpArr.push({
    my.dataGridColumns[0].name: columnData[0]
});
</script>
2
  • Please note where line 16 is. Commented Jan 31, 2012 at 23:44
  • IE9, also happening in Chrome 16.0.912.77. Commented Feb 1, 2012 at 15:00

1 Answer 1

3

In JavaScript, you can treat an object like an associative array using []. You can take advantage of this to access property names dynamically.

How about:

var obj = { };
obj[my.dataGridColumns[0].name] = columnData[0];

tmpArr.push(obj);
Sign up to request clarification or add additional context in comments.

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.