I need to create an object in javascript, similar to the following structure:
{
'1': {
'key1-1': 'val1-1',
'key-1-2': 'val1-2'
},
'2': {
'key1-1': 'val1-1',
'key1-2': 'val1-2'
}
}
I tried with Object(); but the key remains the literal name of the variable.
> var myObj = new Object();
> for (var i = 1; i< 5; i++) {
... myObj.i = {'key1-1': 'val1-1',
..... 'key-1-2': 'val1-2'}
... }
> console.log(myObj)
{ i: { 'key1-1': 'val1-1', 'key-1-2': 'val1-2' } }
Tried converting the variable i to string but still the same.
> for (var i = 1; i< 5; i++) {
... var iStr = i.toString();
... myObj.iStr = {'key1-1': 'val1-1',
..... 'key-1-2': 'val1-2'}
... }
> console.log(myObj)
{ iStr: { 'key1-1': 'val1-1', 'key-1-2': 'val1-2' } }
How can we create a json Object with numeric string keys like '1', '2', '3' ...
myObj.itomyObj[i]