Tips on extracting JSON subcategories?

I am currently attempting to retrieve information from a local JSON file using the following JavaScript script:

let accordion=document.querySelector('#accordion');
fetch('countries.json')
.then(function(response){
    return response.json();
})
.then(function(data){
    let continents=Object.keys(data);
    for(let i=0;i<continents.length;i++){
        let continent=continents[i];
        let heading=document.createElement('h3');
        heading.textContent=continent;
        accordion.appendChild(heading);
        let countries=data[continent];
        let ul=document.createElement('ul');
        for(let j=0;j<countries.length;j++){
            let country=countries[j];
            let li=document.createElement('li');
            li.textContent=country;
            ul.appendChild(li);
        }
        accordion.appendChild(ul);
    }
})

The current outcome is displaying the header of the "continents" data along with the number of rows correctly. However, the content within the array (which comprises all of the data) is showing as [Object Object]. Therefore, I am looking to properly fetch this data and also access the data within the array objects.

Answer №1

It was astonishingly easy to fix the issue at hand... thanks to Bammar's suggestion, I realized I had mislabeled my data

All it took was li.textContent = country.name

Interestingly, what I thought was country data was actually continent data, but that's been resolved now and I'll be using forEach loops to retrieve the correct country information.

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

Sending a JSON object in Swift without using the traditional "key - value" pair formatStringEncoding in Swift without the usual "key

Hello there, I'm facing an issue where I need to send a token string to the Django-server but it only accepts one string at a time. I've been attempting to use alamofire for this task, however, I'm unable to send a key-value pair to solve th ...

How can I match the date format of d-M-Y using JavaScript regex?

When it comes to date formatting in PHP, I typically use the format d-M-Y. Recently, I attempted to match these dates using a JavaScript regex: s.match(new RegExp(/^(\d{1,2})(\-)(\w{3})(\-)(\d{4})$/)) I wanted to use this regex w ...

Performing HTTP requests within a forEach loop in Node.js

I have a collection of spreadsheets containing data that needs to be uploaded through an API. Once the data is extracted from the spreadsheet, I create individual objects and store them in an array. My initial approach involved iterating over this array, ...

How come the Google Calendar API in my Chrome extension doesn't update to display new events when I refresh the page after adding them through gcal?

the query I'm facing an issue with displaying upcoming Google Calendar events on my Chrome extension using JavaScript and the Google Calendar API. The problem arises when I add new events on the Google Calendar website itself, as these events do not ...

Determining the nearest color match to a specified RGB value in C programming

Discover an intriguing website showcasing a collection of 20 simple and unique colors: I'm currently developing a program designed to identify colors and assign them a corresponding name. The challenge lies in creating a function that can: Accep ...

Is there a way to retrieve the line number of an error within a dynamically inserted <script> element?

When I dynamically create a script element and add it to the page, the errors do not give me the line numbers of the script itself, but instead provide the line number where I append the script. The following code in a .js file will result in an error mes ...

Combining React with Typescript allows for deep merging of nested defaultProps

As I work on a React and Typescript component, I find myself needing to set default props that include nested data objects. Below is a simplified version of the component in question: type Props = { someProp: string, user: { blocked: boole ...

The process of masking a video with alpha data from another video on canvas seems to be experiencing a

I have a unique setup on my page where I'm masking one video with another. Essentially, when the Play button is pressed, a second video slowly appears over the looping video in the background. This effect is achieved by using a black/white mask transf ...

Tips on placing a button at the bottom of the AppBar and keeping it fully responsive

I am attempting to place my login and log out buttons at the end of the AppBar. When I try forcing it with marginLeft: '70%',, it works but only on large resolutions. On smaller resolutions, the buttons go out of place. Here is an example of forc ...

Instructions on how to include a conditional click attribute to a hyperlink

Is there a way to make an anchor tag trigger a function only if a specific variable is set? In this scenario, the variable name is assigned the value of "Shnick", so when the link is clicked it should activate the func() method. However, clicking the link ...

Guide on accessing POST data in jQuery

Similar Question: how to retrieve GET and POST variables using JQuery? This is the HTML snippet I am working with: <form action='.' method='post'>{% csrf_token %} <div class="parameters"> Show & ...

Tips for resolving the Angular Firebase v.7 problem: The search for '_delegate' in the users/xxxxx cannot be conducted using the 'in' operator

I'm working on implementing the new Angular Firebase v.7 with Angular and encountering an error message: Cannot use 'in' operator to search for '_delegate' in users/1QAvZYg6aqe0GhA13tmVAINa. While I found a similar question ( Fire ...

Controller function failing to trigger

I'm new to asking questions, so if I've made a mistake, please let me know. I've searched for an answer here but couldn't find one. Any help would be greatly appreciated. I'm attempting to load a list of "Countries" using a contro ...

Google is currently unable to provide the accurate latitude and longitude of the current position

I'm looking to incorporate geolocation functionality into my Laravel project. I've implemented this code, but it seems to be giving me slightly different latitude and longitude values. function getLocation() { var x = document.getElementById( ...

Exploring the Overlapping of Objects in Three.js

I am dealing with multiple meshes in my code and I am trying to figure out how to make the renderer display the object that should be in front, somewhat similar to what is shown in this example: --> . I have read about render depth, but I am unsure of h ...

Surprising outcome arising from simultaneous execution of numerous asynchronous operations on every individual object within an array

I'm fairly new to working with Node.js and I'm still trying to grasp the concept of callbacks and the asynchronous nature of Node.js. However, I've encountered a problem that I can't seem to solve. I've already searched extensively ...

Verify whether an item exists within a group of objects, and if so, eliminate it from the group; otherwise, include it in the group

I am working on a function to create an array of objects where only specific attributes are kept from the original object. The goal is to check if the selected object is already in the array - if it is, remove it; if not, add it. However, I'm facing ...

Aggregate the properties of objects in an array into a single object using Lodash

I've been struggling to figure this out on my own, so I decided to seek advice from those with more experience. I have an array of objects called items, and I need to sum up specific properties across different objects in the array. The user can selec ...

Troubleshooting: xPages radio button group onchange event malfunctioning

I have created a simple page with a listbox control that should update its values based on the selection made in a radiobutton group. The listbox is connected to a scope variable array as its data source. However, I am experiencing an issue where the lis ...

Guide to deploying a Node.js application with a Gruntfile on Heroku

I have encountered an issue while trying to deploy my Node.js application to Heroku using the BUILDPACK_URL. Despite searching for a solution, I have been unable to find a suitable answer. Here is the procedure that I followed: heroku create myapp hero ...