Developing a JavaScript Function to Traverse JSON Data

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]);       
    }
}

Answer №1

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

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the best way to display alert messages using the alert method in Node.js with Jade?

Is there a way to render a 'Jade view' with data? For example, in the following code: app.get('/', function(req, res){ res.render('alert', {msg: 'hi!'}); }); I attempted to write the Jade code below: (alert.ja ...

Why doesn't angular generate ng-reflect-_opened="false" in its production build?

We are currently utilizing the following technologies (which cannot be changed in the near future): Angular 2 RC5 Angular CLI 1.0.0-beta.10 Material Design Side Nav Control Node 6.9.1 npm 3.10.8 Windows 10 When we compile the code (using ng serve with d ...

Tips for executing Cross-Origin-Requests from Firebase Hosting to an AWS Lambda function

Excuse me, I'm still new to web development. I have a basic single-page website hosted on Firebase hosting that retrieves some information from an AWS Lambda function (I didn't use Google Cloud because it didn't support outbound requests fo ...

Error: The object with the memory address 0x7f3f4bd01470 returned by the neo4j.work.result.Result is not compatible with JSON serialization

Hello, I am facing an error with the following code and need assistance in resolving it: class GenerateQuery: @staticmethod def get_nlg(graph_query): # graph = Graph("http://localhost:7474",auth=("neo4j", "pass" ...

What is the method to select a hyperlink that includes a variable in the "href" attribute and click on it?

Currently, I am in the process of creating acceptance tests utilizing Selenium and WebdriverIO. However, I have encountered a problem where I am unable to successfully click on a specific link. client.click('a[href=#admin/'+ transactionId + &apo ...

Page for Managing Users

I've been working on a practice website that includes a page for managing users. All of the data validation in JavaScript and PHP has been completed, but I have encountered an issue with user management actions: Add, Update, Delete. When adding a new ...

Preventing duplication of code execution in recycled PHP elements

Currently, I have implemented a generic toolbar that is used on multiple pages of my web application by using PHP include. This toolbar contains log in/log out functionality and checks the log in status upon loading to update its UI accordingly. Initially ...

Guide on scheduling a daily API GET request in a Node.js script for 11:00pm

I am working on a node js application that involves making an AWS API GET call. http://localhost:3000/amazon/api Within this call, I have specified the necessary functionalities. My goal is to automate this call to run everyday at 11:00PM using node js ...

Issues with JQuery scroll() / scrollTop() functionality in Internet Explorer and Firefox

Our current script showcases a dotted line starting from the top of the screen leading to an up arrow. The position of the arrow changes based on how far the user has scrolled down the page, allowing them to click on the arrow and scroll back to the top. W ...

Performing an AJAX request to the database when a change occurs, prior to submitting the

In my user setup/create form, I am including a field for the users' license/certification number which needs to be validated and returned to a specific DIV Onchange before the form is submitted. I have heard that using AJAX POST is the way to achieve ...

Enhance the deserialization process in ASP.NET Web API

Having developed a Webserver using OWIN / Katana, I am currently facing challenges with grasping the JSON deserialization process... The following is a simple POST method: public IHttpActionResult Post([FromBody] Person person) { // do some stuff return ...

Module 'git-host-info.js' not found

I am currently working on following the official Angular update instructions provided here from version 6.0 to 7.0. However, I have encountered an error. The error message states that my global Angular CLI version is higher (8.2.2) than my local version ( ...

Tips for customizing the name and description of your Firefox SDK Add-on for localization

Currently, I am working on creating a Firefox add-on and am looking to localize the name and description of the add-on. This information is displayed to users in the Add-ons Manager menu. Unfortunately, the resources I have found online only cover prefere ...

The CSS transition fails to function correctly when rendering a React element within an array

When rendering a React component within an array using CSS transitions, I noticed that the elements in the array re-order and change style. Surprisingly, only the elements moving up have transitions applied, while the ones moving down do not. I expect all ...

Utilizing Actionscript3 to efficiently parse JSON data using an object-based approach

I am working on a flash app that requires me to parse a JSON object passed by an external API, which unfortunately cannot be modified. The structure of the JSON data is as follows: { "products": [ { "title": "test", " ...

Exploring the possibilities of the Facebook Graph API search functionality

In order to implement a search feature in my application, I am utilizing the Facebook Graph API search option. After obtaining an access token, I am able to retrieve a JSON array when directly entering the URL into the address bar. However, when attempting ...

creating circular shapes with canvas functions

Creating a circle in my game seems to be more challenging than I anticipated. While there are numerous tutorials available, none seem to address my specific issue. The problem lies in where and how to draw the circle within my existing code: function start ...

Is there a Vue.js alternative to Express-Handlebars sections?

After starting a project using Express and Handlebars, I was advised to explore Vue.js. As I am still in the process of going through the documentation, I find it challenging to grasp how layouts, partials, and sections work in Vue.js. It seems like a part ...

What is the best way to create a toggle button that can show more or show less of a text snippet with animation?

Is it possible for someone to assist me with showing a long text partially and providing a "show more" button to reveal the rest, along with a "show less" option, all with some CSS animation? I was thinking of using a font awesome arrow down icon for expan ...

Is there a way to ensure the functionality of this code without exposing my API keys? I am currently developing an application using Node.js, Angular, and Express

I have come across an issue with my code where process.env is not working within a specific function. I need to find a way to call process.env without displaying my keys. If I don't add my keys, the API call won't function properly. What options ...