I have the below code to push new objects to an array and display the JSON:
let paragraph_json = [];
for (let paragraph = 1; paragraph <= 3; paragraph++) {
paragraph_json.push({
text: `this is paragraph ${paragraph}`,
});
}
res.json({
data: paragraph_json,
});
and I get the following JSON:
{
"data": [
{
"text": "this is paragraph 1"
},
{
"text": "this is paragraph 2"
},
{
"text": "this is paragraph 3"
}
]
}
However I need something like this:
{
"data": [
"1": {
"text": "this is paragraph 1"
},
"2": {
"text": "this is paragraph 2"
},
"3": {
"text": "this is paragraph 3"
}
]
}
Where the paragraph number ("1", "2", "3"...) is appended before the node (opening curly braces) of that paragraph.
Any idea how can it be done from within the loop? Sorry, I'm a beginner and any code that I try basically gives me syntax error.
Slight changes in the array/json type would be fine.