I am wondering why the code below is invalid. It gives the following error: SyntaxError: missing ] in computed property name. If I remove the outer curly braces it works. But I am wondering why it wouldn't work with the curly braces.
const example2 = {[
{
'title': 'Hello World',
'Author': 'John Doe',
},
{
'title': 'Hello World2',
'Author': 'John Doe',
},
]};
console.log(example2);
Objects have key/value pairs. Your example2 object had an array (value) with no key.
const example2 = {"array" : [
{
'title': 'Hello World',
'Author': 'John Doe'
},
{
'title': 'Hello World2',
'Author': 'John Doe'
}
]};
console.log(example2);
You're putting an array inside an object, without defining the key (or the property).
Object is a key/value pair, you can't give a value without a key.
var myObj = {
myArr: [{
'title': 'Hello World',
'Author': 'John Doe',
},
{
'title': 'Hello World2',
'Author': 'John Doe',
},
]
};
You are getting this error:
SyntaxError: missing ] in computed property name
Because there is an error with the Object initializer syntax.
An object can be created using the “key: value” pair.
And in your case, you have initialized example2 object without the key.
const example2 = {
"key": [{
'title': 'Hello World',
'Author': 'John Doe',
},
{
'title': 'Hello World2',
'Author': 'John Doe',
},
]
};
console.log(example2);