I need a different order of the values of my JSON string.
I use php to create a JSON string from a database query that looks like this:
$lbtbs = $DB->get_records('lbtb_eintraege');
$lbtb_json = json_encode($lbtbs, JSON_PRETTY_PRINT);
The database is read out in the “Moodle” system.
According to the documentation an array of objects is returned.
My JSON string that I submit using php looks like this:
{id: '1', date: '2024-05-05', start: '1', description: 'fdsfdsfd', standort: 'sz', …}
In Javascript I parse the JSON string with:
this.events = JSON.parse(decodeURI(this.lbtbdata));
However, I need the string to be nested differently. The date should come first, then the ID and the remaining values within the ID.
As an example:
[DATE]
- [ID]
- [REST DATA]
I tried it like this.
for (const date of Object.keys(this.events)) {
for (const id of Object.keys(this.events[date])) {
const event = new Event(this.events[date][id]);
this.events[date][id] = event;
}
}
I’m still missing a few exercises when it comes to JavaScript. I would therefore be grateful for any suggestion for a solution or for a few keywords to search for!