Reputation: 139
I am wondering which is actually the better method of handling this data in JSON. Especially working with IONIC
var quizs = [
{id: 1,
question: '1.jpg',
desc: 'What color is displayed here',
answer: 'blue, green, orange'}
]
or this
var quizs = [
{id: 1,
question: '1.jpg',
desc: 'What color is displayed here',
answer: [{
color: 'blue',
color: 'green',
color: 'orange'}]
]
The second one seems to be too much in repeating.
Upvotes: 0
Views: 75
Reputation: 72837
The second one isn't a valid object.
An object can't have multiple properties with the same name.
An option is to (properly) use an array, instead:
answer: ['blue', 'green', 'orange']
Using an array will have the advantage that you won't have to transform the answer
to get one of the possible answers out of it.
To get "blue"
, your first example would require:
quizs[0].answer.split(', ')[0]
Where the array wouldn't need the split
:
quizs[0].answer[0]
Upvotes: 1