Retrieve the Array Object's Name in a JSON File

I am looking for city names like Salem and Madurai from the given JSON data

{
    "status": "success",
    "DisplayList": [
        {
            "AVINASHI": [
                "gmail@com",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f0faf6fefbd7ffe7b9f4f8fa">[email protected]</a>"
            ]
        },
        {
            "AVINASHI": [
                "gmail@com",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91f6fcf0f8fdd1f9e1bff2fefc">[email protected]</a>"
            ]
        },
        {
            "ERNAVOOR": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691a1b00290e04080005470a0604">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="592a2b30193e34383035773a3634">[email protected]</a>"
            ]
        },
        {
            "ERNAVOOR": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681b1a01280f05090104460b0705">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f7f6edc4e3e9e5ede8aae7ebe9">[email protected]</a>"
            ]
        },
        {
            "HYDCURD": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c6f6e755c7b717d7570327f7371">[email protected]</a>"
            ]
        },
        {
            "KANCHIPURAM": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7c4c5def7d0dad6dedb99d4d8da">[email protected]</a>"
            ]
        },
        {
            "KEELKATTLAI": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295a5b40694e44484045074a4644">[email protected]</a>"
            ]
        },
        {
            "MADURAI": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c2f2e351c3b313d3530723f3331">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6417160d56240309050d084a070b09">[email protected]</a>"
            ]
        },
        {
            "MADURAI": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a2a3b891b6bcb0b8bdffb2bebc">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b787962394b6c666a626725686466">[email protected]</a>"
            ]
        },
        {
            "SALEM": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f78727e76735f776f317c7072">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="11767c70787d5179613f727e7c">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47202a262e2b072f37766924282a">[email protected]</a>"
            ]
        },
        {
            "SALEM": [
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="51363c30383d1139217f323e3c">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98e84888085a98199c78a8684">[email protected]</a>",
                "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="137474753482479bbefad884759"><
            ]
        
.

Answer №1

let dataList = jsonObject['DataList'];
for(let i = 0; i < dataList.length; i++)
{
    for(let key in dataList[i])
    {
        console.log(key); //prints out names
        console.log(dataList[i][key]); //prints out array of emails
    }
}

If you have the JSON information stored in variable jsonObject

Answer №2

const data = { status:'success', items: [{...}, {...}]} // sample object

let itemLength = data.items.length,
    output = [];

while (itemLength--){
    output = output.concat(Object.keys(data.items[itemLength]));
}

console.log(output.join(", "));

This method utilizes the Object.keys function to list the properties of an object without including prototype properties. It is compatible with IE9+ but may require a plugin for older browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Compatibility

Answer №3

Experiment with this code snippet:

for(var i in json.DisplayList) //Iterating through the DisplayList Array
    for(var k in json.DisplayList[i]) // Accessing objects within the array
        console.log(k); //Printing out the key of each object

Answer №4

Check out this code snippet:

let dataList = JSONObject['DisplayList'];
let uniqueNamesList = [];
for(let i=0; i< dataList.length; i++)
{
    for(let key in List[i])
    {
        if(uniqueNamesList.indexOf(key) === -1)
        {
          uniqueNamesList.push(key);
        }
    }
}
console.log(uniqueNamesList); //Output: ['AVINASHI','ERNAVOOR','HYDCURD',...] 
//The array will only contain unique names

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 is the best way to represent boolean values in yason encoding?

I am currently exploring the yason library to encode data for an API endpoint. Within the return value, I have some booleans that need to be properly handled. The issue arises when dealing with boolean values. cl-user> (yason:encode nil) null NIL cl-us ...

Switching images dynamically using Flask and JavaScript

I'm currently working on Flask and encountering a perplexing issue. I'm attempting to update an image using JavaScript, but I am getting these errors from Flask: ... 12:05:34] "GET / HTTP/1.1" 200 - ... 12:05:38] "GET /img/pictur ...

jquery accordion failing to display content

Upon navigating to my website and accessing the webpage featuring an accordion, the accordion successfully appears and hides after 1500ms as intended. However, when attempting to reveal the text by clicking on the headings, nothing happens. The heading sim ...

Vue: Ensuring one method finishes executing before triggering the next one

I've implemented two methods in my Vue instance; const app = new Vue({ el: '#app', data: { id: null }, methods: { getId: function() { return axios.get('url') .then(response => response.data) .then(i ...

"Encountering issues with Rails and AJAX where the data returning is showing up

I am facing a challenge while trying to use AJAX in Rails to POST a comment without using remote: true. I am confused as to why my myJSON variable is showing up as undefined, while data is returning as expected. Check out my code below: function submitVi ...

Ensure the initial value in the dropdown menu is automatically set to the first value in VueJs

How can I set the first value of my time object as the default value for my dropdown in Vue? I want the first value to be pre-selected when a user enters the website, but currently only display the value without actually selecting it. time Object:- { &quo ...

What is the best way to dynamically insert an object into a field name in react-final-form?

When using the react-final-form component, you can expect the following result: <Field name="answers[0].name" component="input" type="radio" value="0" /> { answers: [ { name: 'value' } ] ...

Electron experiences a crash while attempting to execute an HTTPS request within an addeventlistener callback function

In the process of creating a simple Electron application that facilitates user login into a system, I encounter an issue. The app collects the username and password entered by the user through form text inputs. Upon clicking the "login" button, the program ...

Is there a way to only retrieve the title and artist information from the data?

I am currently working on extracting the titles and artist from a JSON page. While I can successfully retrieve all the entries, I'm having some difficulty in isolating just the title and artist information. Below is the snippet of code I am using to a ...

Can you please point out the location of the error in the Java Script code

Our application is developed using Yii2 framework and incorporates Kartik's star-rating widget. However, there seems to be an error in the JavaScript code: Error message from console: Check out the problematic code snippet: <?php $js = ...

Unable to render text onto an html5 canvas

Currently delving into JS after having experience in other programming languages. My focus right now is on creating a canvas that can display text. https://jsfiddle.net/b5n2rypn/ The issue at hand: Despite using the fillText method, no text appears on th ...

I would like for my element to increase and decrease in size while rotating when the button is clicked

I am trying to create an element that changes its size while spinning when a button is clicked. It seems to be working fine on hover, but not onclick. Here is the code I have: <div class="rec">Rec</div> <button id="button&quo ...

Obtain information from a specific list item using Jquery

As a beginner in the world of coding, I've been making great progress with your assistance and I'm excited to continue learning by tackling my next challenge! I've successfully created a JQuery list that displays dummy information from a lo ...

What are the best ways to create image animations on top of other images using CSS or JavaScript?

Imagine if the first image is in black and white, while the second one is colored. How can we make the black and white image change to color after a timeout period, with an animation similar to loading progress bars? Is this achievable using CSS or JavaScr ...

Managing data binding for checkboxes within a constantly changing set of options

I'm currently working on designing an angular directive for selecting items from a categorized list. Each item in the list should be selectable using a checkbox. The input data that will be provided to the directive looks something like this: [ { ...

Having trouble retrieving text using getText() or getAttribute("innerHTML")

getAttribute but struggling to retrieve the text associated with each combobox. I need to access the text content of every combobox. <small> <input type="checkbox" checked="checked" value="on" name="irOperations[0]"/> Account Activity < ...

Creating an Array of dynamically generated elements using jQuery

A dynamic table is being generated using jQuery's .append() function: $el.append('<tr data-id=' + data[i].id + ' data-token=' + data[i].token + '><td>' + data[i].order + '</td>\n\<td&g ...

JavaScript maintaining records and connections between multiple asynchronous events

I am working on an Angular 10 project where users can compress and upload images to Google Cloud Storage. The compression and uploading functionalities are functional, but I'm facing challenges with managing the asynchronous process of handling multip ...

Most effective method for exporting data to a JSON file

I possess an array of weather data for various "stations," with most being situated at airports. This data has been gathered from web APIs and is currently presented as unordered arrays. As an example: Station: Chicago (O'Hare Airport) Temperature: ...

How can I prevent media controls from appearing in fullscreen mode on Microsoft Edge using CSS?

My video code allows for switching the video into fullscreen mode using custom controls. Here's how it looks: fullScreenButton.addEventListener("click", function() { if (video.requestFullscreen) { video.videoContainer.r ...