gpt4 book ai didi

javascript - 创建一个通过 JSON 进行迭代的 Javascript 函数

转载 作者:行者123 更新时间:2023-12-02 16:44:54 25 4
gpt4 key购买 nike

如何访问 JSON 中键:值对的值?现在,该函数迭代 json 并提供键,但不提供相应的值。举例来说,如果我想要每个员工每项任务的站点值(value),我该怎么做?

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 gives me the object, I would like the employee number (eg:1000)
console.log([i]);
for (var j in s["schedule"]["employees"][i]["tasks"]) {
//how do I print the value for the "site" key?
console.log([j]);
}
}

最佳答案

console.log(i); // employee number

console.log(s.schedule.employees[i].tasks[j].site); // task site

关于javascript - 创建一个通过 JSON 进行迭代的 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com