I want to be able to create and object like this
{'someKey':
[{'someSubKey1': 'value1'},
{'someSubKey2': 'value2'},
{'someSubKey3': 'value3'}]
}
I tried a lot but I think I'm missing something
first of all I created an array and pushed the elements, then I did a JSON parse and finally a join (","), but the result wasn't the expected.
Some help?
2 Answers 2
You need to put the contents in an array with []. Wrap you inner json inside an array like
var obj = {'someKey': [
{'someSubKey1': 'value1'},
{'someSubKey2': 'value2'},
{'someSubKey3': 'value3'}]
}
console.log(obj);
answered Feb 1, 2017 at 20:47
Abhinav Galodha
9,9862 gold badges33 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
NewJsGuy
I feel like an idiot, I miss the [ ] in the original post, I edited it now
If you need to access your data structure like myvar.someKey.someSubKey1, consider this structure:
{ 'someKey':
{
'someSubKey1': 'value1',
'someSubKey2': 'value2',
'someSubKey3': 'value3'
}
}
If you'd like to go with myvar.someKey[0].someSubKey1, try this:
{'someKey':
[
{'someSubKey1': 'value1'},
{'someSubKey2': 'value2'},
{'someSubKey3': 'value3'}
]
}
answered Feb 1, 2017 at 20:51
Alex Svetkin
1,42715 silver badges17 bronze badges
Comments
lang-js
someSubKey1,someSubKey2, etc all be part of the same object? I recommend to read a tutorial about data structure in JS: eloquentjavascript.net/04_data.html . Also, what has JSON to do with it?I tried a lot. Could you please put the codes you tried?