Traversing a JavaScript class within a for loop

I am attempting to access the locations listed in json.responseJSON.Sites, starting with LHR on the first iteration and then NJE on the next one, and so forth. The notifications for each location are "LHR" and "NJE", respectively. Is it possible to achieve this?

    for(var notification in json.responseJSON.Sites){
            console.log(json.responseJSON.Sites.notification);
    }

This is the structure of my JSON data:

{
"Sites": {
    "LHR": 1,
    "NJE": 1,
    "AZS": 1,
    "SGP": 1,
    "OHS": 1,
    "AZP": 1
  }
}

Answer №1

If you need to access it using property access notation (instead of object.property, use object['property']) like this:

  responseData = {
"Sites": {
    "LAX": 1,
    "JFK": 1,
    "ORD": 1,
    "ATL": 1,
    "SFO": 1,
    "BOS": 1
  }
};

for(var city in responseData.Sites){
    console.log(city + " : " + responseData.Sites[city]);
    }

Fiddle: https://jsfiddle.net/5jgqk43s/2/

Answer №2

Iterating over notifications in the Sites object of json.responseJSON:
for(var notification in json.responseJSON.Sites){
        console.log(notification);
}

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

Modifying the theme of the Angular UI-Bootstrap datepicker

I am currently facing an issue with my angular datepicker, which is appearing oversized and covering almost 30% of the screen. Additionally, there are large gaps between the dates in the calendar view. After some investigation, I believe this problem may ...

The set method of useState is not causing the specific component to render

My concern arises when trying to update the TaskDetail component located at Right Side. Despite changing the value of the card within the handleCardClick callback, the specific component does not reflect this change. In the provided gif, it can be observed ...

Converting poorly formatted MySQL dates into JSON using ASP

Thrown into a new project, I discovered that the opening hours are stored in a bizarre DB pattern. This has been driving me crazy for over a week. Check out this image: https://i.sstatic.net/aUEqm.png The current format of saving opening hours is as fol ...

Problem encountered while attempting to sort a table with JavaScript and jQuery

Having trouble sorting my table by the content in the first column. I've tried implementing code similar to the one found in this ANSWER, but unfortunately, it's not working as expected for me. When you run the snippet, you can see that the table ...

Tips for managing large amounts of data retrieval from an API

As a beginner, I am attempting to retrieve data from an API and display it using the v-for directive. However, this process is causing my app to lag. It freezes when new data is fetched or when I search within the list. The following code snippet shows whe ...

What is the most strategic way to conceal this overlay element?

Currently, the website I'm developing features a series of navigation elements at the top such as "Products" and "Company." Upon hovering over the Products link, an overlay displays a list of products with clickable links. Positioned at the top of the ...

Permission for geolocation must be granted every time when using Safari and mobile browsers

My website requests geolocation permission but has a recurring issue. When accessed on mobile (using Chrome or Safari) or desktop Safari, the permission prompt pops up every time a page is reloaded. However, when accessing the site on a computer with Chro ...

A guide on implementing React hooks within a React class component

Just stepped into the JavaScript world and facing a bit of trouble... Currently, I'm working with a React hook import { useKeycloak } from '@react-keycloak/web'; import { useCallback } from 'react'; export const useAuthenticatedCal ...

What is the best way to connect a Jquery plugin to a table within a partial view following an ajax request?

I have a main view that utilizes a partial view to display items in a table format. Main View (Enclosed within a div, it references the Partial View) <div id="divList"> @Html.Action("_list") </div> Partial View (Displaying a list of it ...

Obtain information from an HTTP GET call

I have encountered an issue where I am unable to access the data that is passed with an HTTP GET request from the client to my server. Oddly enough, this setup works perfectly fine for POST requests but not for GET requests. The technologies being used ar ...

The issue with Jquery Lightbox/Modal doubling upon clicking

Having just started with jQuery, I've encountered an issue with my Gallery Lightbox/Modal. Whenever I click on a thumbnail, the modal reopens itself and I'm unsure where to set a reset. Any help would be greatly appreciated. jQuery(function($) { ...

Determining when all textures have successfully loaded in Three.js and ensuring there are no lingering black rectangles

I'm currently developing a web platform that allows users to customize and preview 3D house models. If the user's browser doesn't support WebGL, the server renders the house and sends screenshots to the client. However, if the screenshots ar ...

Fixing PNG Fading Issue in Internet Explorer 8

I am currently utilizing the jQuery Cycle plugin to produce a fading animation effect. While this works perfectly on most browsers, I have encountered an issue with PNG files containing semi-transparent pixels in Internet Explorer 8. These partially transp ...

One function is failing to execute properly due to multiple if conditions

Take a look at my JavaScript code below: function showLater1() { if ((vidos.currentTime >= 30) && (vidos.currentTime <= 34)) { lay.style.opacity = "1"; content.style.opacity = "0"; controls.style.opacity = "0"; ...

Adding a specific element to an array using JQuery

I am in the process of developing a web application using jQuery to track goals and habits. Users can set a main goal such as 'Discipline' and then add sub-goals or habits related to that main goal (e.g. 'exercise every day'). Organizi ...

Issues with loading JavaScript in Selenium

I have been trying to extract data that is loaded through google tag manager. Despite my efforts using both chrome and firefox, the li elements I need in the body always appear empty no matter what timing I use. import undetected_chromedriver as uc from ...

JSON arrays that are nested within one another

I am currently working on parsing some JSON data that contains nested arrays, and I'm facing difficulties extracting the data from the inner arrays within the main array. This is a snippet of how my JSON data is structured: {"TrackingInformationResp ...

Choosing the primary camera on a web application with multiple rear cameras using WebRTC

Having a bit of trouble developing a web app that can capture images from the browser's back camera. The challenge lies in identifying which camera is the main one in a multi-camera setup. The issue we're running into is that each manufacturer u ...

Updating a .txt file using JavaScript

Is there a method on the client side to add text to a file called text.txt using JavaScript? In Python: f = open("text.txt","w") f.write("Hello World") f.close() The code snippet above would input "Hello World" into the text file. I am looking for a sim ...

Canvas - Occasionally, using lineTo() can result in crisp edges in the drawing

I've created a simple Canvas drawing app. However, I've noticed that occasionally the lineTo() command generates a line with fewer coordinates, resulting in many edges: I'm currently using the latest version of Firefox - could this issue be ...