Traverse through nested objects

I am currently working with JSON data containing information like uf, ivp, and others. My goal is to iterate over all the objects in order to access their properties.

{
"version": "1.5.0",
"autor": "mindicador.cl",
"fecha": "2018-10-31T13:00:00.000Z",
"uf": {
"codigo": "uf",
"nombre": "Unidad de fomento (UF)",
"unidad_medida": "Pesos",
"fecha": "2018-10-31T04:00:00.000Z",
"valor": 27432.1
},
"ivp": {
"codigo": "ivp",
"nombre": "Indice de valor promedio (IVP)",
"unidad_medida": "Pesos",
"fecha": "2018-10-31T04:00:00.000Z",
"valor": 28540.81
},
}

Currently, I have a code snippet that only gives me the names of the objects but I am unable to access their properties:

$.ajax({
                type: 'GET',
                url: API_REQUEST,
                dataType: 'json',
                success: function(data) {
                    $('#mindicador').html(JSON.stringify(data));
                    let k;
                    for (k of Object.keys(data)) {
                        if (k === 'version') continue;
                        if (k === 'autor') continue;
                        if (k === 'fecha') continue;
                        console.log(k.nombre);
                    }
                }
                ,error: function(jqXHR, textStatus, errorMessage) {
                    console.log('errorMessage: ' + errorMessage);
                }
            });

Answer №1

Instead of simply looking at an array of string keys by iterating through the Object.keys of data, you can actually access the values associated with those keys using bracket notation on the original data object:

for (const key of Object.keys(data)) {
    console.log(data[key]);
}

Alternatively, you could iterate over the object itself using a for ...in loop rather than using Object.keys:

for (const key in data) {
    console.log(data[key]);
}

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

Exploring the Dynamic Routing Features of Next.js

After diving into Next.js, I found the learning curve to be manageable since React was already familiar territory for me. Currently, my focus is on implementing dynamic routing in my application - Starting with my live/index.js file - (Where I utilize ge ...

Transforming strings of HTML into objects in the DocX format

After developing a TypeScript script that transforms a JSON string into a Word Doc poster using Docx, I encountered a hurdle. Certain sections of the JSON may contain HTML tags, such as <br/>, <i>, <p>, and I need a way to pass the stri ...

Angular's $location is reverting back after the modification

When using Angular, I encountered an issue where the user was not redirected to the view page after submitting a form. To handle this, I attached the following function to the scope: $scope.create = function () { return $scope.customer.$save({}, funct ...

Ways to update a ViewComponent using Ajax in Asp.net Core 3.1

How can I make FavoriteComponent refresh when the "a" tag is clicked? Html : <div id="favorite-user"> @await Component.InvokeAsync("FavoriteComponent") </div> Action Html : <a id="add-fav" onclick="addfavorite('@pr ...

Send a substantial item for display using Express Layout

Utilizing Express layouts to render my webpage upon accessing a specific route: The current project I am developing is an admin panel designed for editing a substantial table of data (imagine a website dedicated to managing thousands of product entries, fo ...

Error: Unable to find the transport method in Socket.io

I recently implemented user side error logging on my website to track errors. I have noticed that sometimes it logs a specific error related to socket.io code: TypeError: this.transport is undefined This error seems to only occur for users using Firefox ...

Switching out control with a loading image during an ajax request

Looking for help on replacing a drop-down list with a loading image while an ajax call is in progress. After the ajax call is complete, I want to restore the drop-down with all event handlers still active. Any examples or tips would be greatly appreciate ...

The submit button remains disabled even after verifying all the required fields

I have a registration form with basic customer details and a Submit button. I have successfully validated all the fields using Ajax, except for the Confirm password field which is being validated using JavaScript. The issue I am facing is that when I val ...

The Google Software Development Kit has encountered a 403 error, indicating that the user has exceeded their

Currently, I am working on integrating the Google Developer SDK into my project to leverage the Google Translate functionality. While everything works smoothly when making a request using my access token obtained from the Google Developer Console, I keep e ...

Arranging the y-value array based on the x array in ascending order using Python

If I have two arrays like this: x = [0, 7, 2, 4, 6, 9, 5] y = [1, 2, 3, 4, 5, 6, 7] With data points at coordinates such as [0,1], [3,2], and [x_n,y_n]. How can I organize array y so that it corresponds to an ascending order of x values? Meaning the x v ...

Having Trouble Parsing JSON Object with JQuery?

I'm having trouble extracting data from a valid JSON object using jQuery/JavaScript - it always returns as 'undefined'. var json = (the string below). var obj = $.parseJSON(JSON.stringify(JSON.stringify(json))); alert(obj); // aler ...

Exploring the Touch Feature in Angular

I am facing an issue with touch events as I am not receiving any useful X/Y coordinates. The event object does not provide the necessary information I need for touch events (https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches). Despi ...

An issue observed in ASP.NET MVC is that when using the $.post() method, it inadvertently assigns a different controller name to

My ASP.NET MVC application is giving me trouble when I use the $.post() method. This particular method takes the controller name and action name as parameters, like so: "Controller/Action". The issue arises when the post method inexplicably adds the contro ...

I am experiencing difficulty in passing parameters to nested navigators

I have developed a TechInfo page and a ProfileInfo page. The ProfileInfo Page is nested inside a Drawer Navigator. I want to display some information from the TechInfo page and some from the ProfileInfo page when I enter our info and press a button. Howeve ...

How did my attempt to add a length() method to Object end up breaking jQuery?

Here is the code I created: Object.prototype.length = function(){ var count = -1; for(var i in this) count++; return count; } Surprisingly, when viewing my page with Firebug enabled, it gives an error stating that jQuery's .appendTo() is ...

JQuery clockpicker failing to fire change event for input field

While working on a codebase, I encountered a callback binding scenario where a specific action needs to take place whenever any input is altered. $(document.body).on('change', '.input-sm', function (){ ... }) The challenge arises whe ...

Initiating a new PHP session via AJAX request upon initial page load

As I embark on setting up my very first website, I've encountered a puzzling dilemma with PHP sessions. Upon the initial page load in a browser, I include a .php file <head>, and then call another through AJAX. The issue arises when the PHP file ...

What is preventing Angular from loading on this Plnkr?

I've spent time researching my problem and testing various solutions, but I still can't seem to resolve it. Angular doesn't appear to be loading correctly in Plunker for some reason. I've heard that ng-app is no longer supported in ne ...

How to Retrieve a Variable from the Parent Component in a Child Component using Angular (1.5) JS

I am currently working on abstracting the concept of a ticket list building into an angular application using 2 components. 1st component --> ("Smart Component") utilizes $http to fetch data and populate an array called populatedList within the parent ...

Problem with Agile Carousel and thumbnail component

I have incorporated the Agile Carousel plugin into my website to create multiple slideshows. The first slideshow at the top is working fine, but I am experiencing an issue with the carousel in the "About" section at the bottom. While I have successfully se ...