Explore a nested JSON array object to find the number of elements within the array

I have a JSON file with different groups inside the competition, each containing properties like homeTeam and markerImage. I'm looking to access these properties but also need help determining the number of groups in the competition. Can anyone assist me with this?

{
   "competition":{
      "group1":[
         {
            "homeTeam":"Lawrence Library",
            "markerImage":"images/red.png",
         },
         {
            "homeTeam":"Hamilton Library",
            "markerImage":"images/white.png",

         },
         {
            "homeTeam":"Applebees",
            "markerImage":"images/newcastle.png",

         }
      ],
      "group2":[
         {
            "homeTeam":"Lawrence Library",
            "markerImage":"images/red.png",

         },
         {
            "homeTeam":"Hamilton Library",
            "markerImage":"images/white.png",

         },
         {
            "homeTeam":"Applebees",
            "markerImage":"images/newcastle.png",
         }
      ]
   }
}

Answer №1

If you want to find out how many groups are in the competition object, you can do so using the following code:

Object.keys(obj.competition).length

console.log(Object.keys(obj.competition).length);

To iterate through your groups, you can use a loop like this:

for(var x in Object(obj.competition.group1)){
    var newObj = Object(obj.competition.group1[x]);
    console.log(newObj.homeTeam + ' and '+ newObj.markerImage);
}

Answer №2

  1. Extract data from your JSON file and save it in a JavaScript object
  2. Utilize Underscore and Lo-Dash libraries to determine the quantity of objects

Follow this link to learn how to calculate using Underscore and Lo-Dash libraries

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

Arrange an array based on a separate array of value priorities, which could potentially contain more values than the initial input array

I have a list of items in a comma separated string/array: $input = "facebook,steam,tumblr,email,instagram,twitter,twitch,youtube,pinterest,amazon,disqus"; $input = explode(",", $input); that I would like to sort based on another order specified in an arr ...

Stop the repeated loading in Vue.js by implementing load-google-maps-api to ensure the map is only loaded

I am trying to dynamically load two instances of Google Maps on a single page but I keep encountering the following errors: You have included the Google Maps JavaScript API multiple times on this page. This may result in unexpected errors. Below is the ...

Error encountered in React Material UI: Appbar - TypeError occurred when attempting to read properties that are undefined (specifically, reading '100')

Currently, I am working on developing a multi-step form using Material UI components. However, upon importing the necessary components, an error is being displayed in my code snippet: import React from 'react'; import { ThemeProvider } from &a ...

Using Blob to save CSV file on Safari

Here are the codes I am using to generate a download link for users to download a .csv file from my website. var link = document.createElement("a"); link.id = "csvDwnLink"; window.URL = window.URL || window.webkitURL; var csv = "\ufeff" + CSV, b ...

When trying to retrieve JSON data from database entries in PHP, the output may sometimes appear in a strange format. To ensure a proper JSON format

Currently, I am receiving the following JSON: ["1Sales & Consulting","2Pyments","3Investing","4Financing","5Cross Functional"] However, I would prefer to format it as a proper JSON like this: [{"id":1, "name": "Sales & Consulting"},{"id": 2, "na ...

Is the Site Header displayed depending on the scroll position and direction of scrolling?

On my website, I have a header that I want to hide when the user scrolls down 100px and show again when they scroll up 50px. I attempted to write a script for this functionality, but it doesn't seem to be working as expected. CSS /* This CSS rule w ...

Tips on presenting an image from within an MP3 file within an image tag

Hello, I attempted to display the image retrieved from an mp3 file in an image tag using HTML and ReactJS. Here is the code snippet that I utilized. async getImageFromUploadedFile(framesRetrieved){ const imageArrayBufferRetrieved = framesRetrie ...

Is there a way to use a jq command to only show keys that have a value greater than a specified number

Looking at the JSON data below, we see: { "thing_one": 223, "thing_two": 0, "thing_three": 0, "thing_four": 69, "thing_five": 14, "thing_six": 0 } I am trying to extract keys with values greater than 10 using a jq command. The desired output ...

What could be causing an issue with CORS in ExpressJS in one scenario but not in another?

I am currently in the process of setting up a database and connecting it to various routes. Interestingly, I have been successful with one route ('register') but encountering issues with another ('login'). Whenever I attempt to run the ...

How to Assign Values to a Variable from a JSON Response in ASP.NET MVC

I am struggling with populating a variable from a JSON result in my code. I can't seem to find the right keyword to use for it. My goal is to populate this specific variable. Custom Js File var opts = [ { key: 1, label: 'High' }, { key: ...

Field must have a base type specified to proceed

Currently, I am in the process of developing a discord.js bot. The structure I have set up involves a folder that contains all the commands, and when a command is called, index.js directs it to the appropriate file. However, when attempting to run the bot, ...

The output is: [object of type HTMLSpanElement]

<form> <table> <tr> <td>Distance:</td> <td><input type="number" id="distance" onKeyUp="calculate();">m</td> </tr> <tr> <td>Time:</td> ...

Leveraging JQuery to Invoke Partial View

I have a partial view in my /Shared folder that is defined as follows: <div id="myProducts"> @Html.Partial("_ProductsList",Model) </div> I am attempting to refresh the _ProductsList view using jQuery. Specifically, I wan ...

The power of combining React with Three.js

My 3d image is failing to display on the screen. I'm utilizing React with Three.js and encountering the following error message: "THREE.Object3D.add: object not an instance of THREE.Object3D." Below is my component structure: (file tree provided). ...

Guide to fetching external data with Next.js and importing the component into a different file

I have implemented the following code in my pages/github file, and when I navigate to localhost:3000/github, the code runs successfully and returns JSON data. function GithubAPI(props) { // Display posts... return (<div>{props.data.name}</div& ...

List of Tags/Values in the JSON Format

Currently, I am tackling a project that involves assigning multiple values to an object. To elaborate, I need to create a list of 'things' each with a name and additional categorization known as "Tags". These tags could include descriptors such a ...

How can I determine the Windows service pack version directly from my web browser?

Is there a method to determine the installed service pack from the browser? It doesn't seem to be available in System.Web.HttpBrowserCapabilities in asp.net. I am looking for a solution to alert users about the necessity of updating to XP Service Pack ...

Exploring the contents of a dropdown menu by navigating through a tree structure in React JS

A unique custom component has been developed, featuring a dropdown with a tree-like structure inside that allows users to search for values. However, it seems that the search function only works after reaching two levels of the tree structure. Currently, ...

The three.js pointLight has been updated from version 67 to version 68

There appears to be a change in the interaction between a pointlight and a plane from version r.67 to r.68. I am currently studying three.js by following along with a book that is a year old. I have simplified the tutorial example to include just a plane, ...

I've been tasked with utilizing arrays for the storage and organization of objects

I need to develop a storage system for recording fuel transactions made by users when refueling their cars. The system should store the date, car mileage, amount of litres, and cost per litre for each transaction. A separate class must be created to handle ...