0

How do you access the value of a key:value pair in JSON? Right now, the function iterates through the json and provide keys but not the corresponding values. So for instance, if I wanted the site value for each task of each employee, how would I do that?

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]);       
    }
}
1
  • @tokyovariable: He did, needs scrolling though. Commented Nov 28, 2014 at 1:10

1 Answer 1

2
console.log(i); // employee number

console.log(s.schedule.employees[i].tasks[j].site); // task site
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, employees[i] is not valid as employees is not an array. employees is an object and "1000", "2000" are properties of that object.
@RocoCTZ, no its not an array but you can access its properties by using array syntax. And since you cannot do employees.1000 etc you have to do it with the array syntax
@RocoCTZ: ({"1000": "still works"})["1000"] == "still works"
@PatrickEvans So I can see that they're not arrays. But why is is that you cannot do employees.1000 etc and why do you use array syntax if it's not an array?
It's not really "array syntax". It is still object property access. a.b is always identical to a["b"]; but you can't always do the reverse - because some would be ungrammatical (like, e.g. your employees.1000 example), or because the property name is not a constant (i.e. employee.i is wrong because we want the property of employee whose name is not i, but in i). Arrays in JS are just somewhat magic kinds of objects which are optimised to access integral keys, and have a magic property length which is autoadjusted whenever you assign to an integral key.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.