Given the following json object structure, I need to fetch the id and value for each of the goals object as well as planet, people, and theme's object value also.
var data = {
"goals": [{
"id": "goal-09",
"colour": "#FD6925",
"priority": "People",
"theme": "Sustainable Infrastructure",
"title": "Industry, Innovation & Infrastructure",
"value": 4
},
{
"id": "goal-12",
"colour": "#BF8B2E",
"priority": "Planet",
"theme": "Responsible Consumption",
"title": "Responsible Consumption & Production",
"value": 3
},
{
"id": "goal-13",
"colour": "#3F7E44",
"priority": "Planet",
"theme": "Environment",
"title": "Climate Action",
"value": 1
}
],
"planet": 50,
"people": 50,
"themes": {
"Sustainable Infrastructure": 4,
"Responsible Consumption": 3,
"Environment": 1
}
}
I have tried a couple of loops but this is a bit difficult, also you can have up to 17 goals and up to 5 different themes.
Object.keys(data).forEach(function(key) {
alert(key+" "+data[key]); // logs keys in myObject
});
This is where I am testing https://jsfiddle.net/dhzpqo4n/ and I will be fetching the json string from sessionStorage.
the end goal is to construct a sql update statement to store these in a table with the following structure
update I've come up with the following script which is a step in the right direction to construct my sql update statement with the values if each of the goals.
let i = 0
while (data.goals[i].id) {
document.write(data.goals[i].id + "=" + data.goals[i].value + " ");
i++
}
Which gives goal-09=4 goal-12=3 goal-13=1
https://jsfiddle.net/3r49zqsu/4/