Is there a way to access the values of key:value pairs in JSON data? Currently, the function only iterates through and provides keys without their corresponding values. For example, if I need to retrieve the site value for each task assigned to an employee, how can this be achieved?
var s = {
"schedule": {
"employees": {
"1000": {
"task1": [
{
"task": "task1",
"site": "site1",
"from": "0900",
"to": "1000"
},
{
"task": "task2",
"site": "site2",
"from": "0900",
"to": "1000"
}
]
},
"2000": {
"task2": [
{
"task": "task3",
"site": "site3",
"from": "0900",
"to": "1000"
},
{
"task": "task4",
"site": "site4",
"from": "0900",
"to": "1000"
}
]
}
}
}
}
for (var i in s["schedule"]["employees"]) {
// This currently returns the object; how can I get the employee number (e.g., 1000) instead?
console.log([i]);
for (var j in s["schedule"]["employees"][i]["tasks"]) {
// How do I print the value associated with the "site" key?
console.log([j]);
}
}