"Utilizing JavaScript to filter JSON data at a deep nested

When working with a JSON data set, I often face the challenge of filtering based on specific child values. Take the following example:

[
    {
        "Date": "2017-03-02T00:00:00",
        "Matches": [
            {
                "Id": 67,
                "NameSeo": "teste-02-33",
                "IsLive": true
            },
            {
                "Id": 63,
                "NameSeo": "teste-ao-vivo-always",
                "IsLive": true
            }
        ]
    },
    {
        "Date": "2017-03-13T00:00:00",
        "Matches": [
            {
                "Id": 64,
                "NameSeo": "san-avai-13-03-17-13-00-caninde",
                "IsLive": false
            },
            {
                "Id": 66,
                "NameSeo": "teste-01-xxx",
                "IsLive": false
            }
        ]
    },
    {
        "Date": "2017-03-23T00:00:00",
        "Matches": [
            {
                "Id": 65,
                "NameSeo": "gre-cor-23-03-17-17-30-pacaembu",
                "IsLive": false
            }
        ]
    }
]

My goal is to determine how many "Matches" have their "IsLive" value set to true. In this example, the answer should be 2. Essentially, I want to ignore the dates and focus solely on counting live matches.

I've made some attempts using JavaScript filter functions:

x.filter(x => x.Matches.filter(w => w.IsLive)).length

However, this approach returns 3, which includes all the dates as well.

x.filter(x => x.Matches.IsLive).length

Unfortunately, this last method yielded a result of 0.

Answer №1

Give this a shot:

x.reduce((total, item) => total + item.Matches.filter(word => word.IsLive).length, 0)

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

Issues with tangents in three.js compared to the VertexTangentsHelper with problems on display

After enabling the "vertexTangentsHelper" feature in THREE.js, I've noticed that the tangents on various geometries appear to be incorrect. I'm questioning whether these tangents are being miscalculated (possibly due to my shader output) or if t ...

Using Angular 8.x for routing with customized outlet names

I am attempting to set up routing in my Angular 8.x application, utilizing lazy loading and named outlets. Here is the current configuration: main-layout.html <header> <app-top-menu></app-top-menu> </header> <mat-sidenav-cont ...

Error Code: InvalidRequest - Occurs when making a request to a RestAPI using the

When I make a call to a RestAPI and send data in json format using RestSharp as the client, I encounter an issue. Below is the function that I am using: public HttpStatusCode CreatOrder(OrderRequest order,string token) { var client = new RestClient(" ...

The object retrieved from the AJAX call is not defined on the client's server specifically

I am dealing with a web app issue where an AJAX call is functioning as expected on local and test servers, but not on the client's end. The client reported that after some changes were made to the architecture of the app and it was reuploaded to thei ...

Tips on how to toggle the class of one specific element without affecting others

Whenever I click on a div, it expands. However, if I click on a collapsed one, both collapse and the first one returns to an inactive state. At this point, the last two are in both active and inactive states. And if at this time I click on the first one, t ...

How come the values keep appearing without any loops or subsequent calls being made to the PHP file?

Here is a demo example showcasing 'Server Sent Events(SSE)': Embed HTML code(index.html) : <!DOCTYPE html> <html> <body> <h1>Receiving server updates</h1> <div id="result"></div> <script> if(type ...

Can a Node.js dependent library be integrated into Cordova?

After setting up an android project using Cordova, I came across a javascript library that relies on node.js to function. Since Cordova operates with node.js, can the library be invoked within the Cordova environment? ...

employing flush for lodash's throttle wrapper

When using TypeScript with JavaScript and Angular: I am trying to use the throttle decorator from lodash to limit an API call while a user is navigating around the page, ensuring that it fires before they leave the site. In my TypeScript constructor, I h ...

The folder creation in the 'outDir' directory by TSC continues to grow

Hello! Currently, I am engaged in a small TypeScript project where I need to utilize two separate tsconfig.json files, both of which inherit from my main tsconfig.base.json file. Unfortunately, I encountered an issue with the compiler creating unnecessar ...

Child node in Firebase successfully deleted

Is there a method to determine if a subchild has been removed from the database structure below: users:{ a: { friends: { b:Jhon, c:Ted }, b: { friends: { a: Tom } }, c: { friends:{ a: Tom } } } I a ...

Extract quotation marks from a JSON object's value

I am currently utilizing the Woocommerce API within node js to transfer product information from a mssql server database to my woocommerce database. The dataset retrieved through my ms sql query contains an images field. To import images using the Woocom ...

Tips for utilizing the ng-filter Json [@attributes] with Angularjs

I'm trying to sort through sports feed based on sport ID, but the JSON feeds are structured with @attributes. JSON Feeds series: [ { @attributes: { id: "cdf590b4-0206-45b0-9122-20eae46a2b27", name: "Pakistan tour of Sri Lanka, 2015", year ...

The issue arises when attempting to render an SVG with JavaScript embedded inside using the img, object, or

Issue with the title ... please follow these steps: (view codes below) Create an svg + open it separately (name it keysaway.svg) Create html + open it individually When you observe, the svg displays a simple up and down animation but fails to work when l ...

Error: Objects cannot be used as constructors

An Azure Function using JavaScript HTTP Trigger posts activity in a Slack online community for customers. It has been functioning successfully for many years, but after upgrading from version ~2 to ~4, it started throwing an error stating Entities is not ...

Toggle the status of active to inactive instantaneously with the click of a

Incorporating DataTables with ajax using PHP CodeIgniter Framework has presented me with a challenge. I am struggling to toggle between Active and Inactive buttons seamlessly. My desired outcome: When the Active button is clicked, it should transition ...

What is the best way to implement a switch case for the value of a property within an object in a TypeScript file?

The object I'm dealing with looks like this: {a: auth?.type === '1' || auth?.type === '2' || auth?.type === '3' ? { reason: // I need to implement a switch case here : un ...

Attempting to execute a code snippet using Express framework on the local server at port 3000

Having some trouble with my initial attempt at a basic application. The Scraper.js file successfully scrapes a URL and outputs the array to the document object when executed in the console. Now, I want to set up an Express server to run the script whenever ...

Encountering a timeout error while using selenium-webdriver and phantomJS

Currently, I am developing my automated test scripts using a combination of selenium-webdriver, phantomJS, and mocha. The script I am working on is written in JavaScript. One requirement is to wait until a specific element becomes completely visible befo ...

I'm curious about the safety and effectiveness of using a Json object to submit form data. Currently, I'm utilizing MVC 2 in conjunction with JQuery

Recently in my ASP.Net MVC 2 project, I have incorporated the use of a Json object for submitting form data. I am now seeking insights from experts on whether this approach is considered safe and recommended, and why. To clarify, I am not asking for tech ...

Invoking Ajax within a for loop

for (var i = 0; i < 5; i++) { using (x = new XMLHttpRequest()) sendRequest("GET","d.php?id=" + i), checkResponse(null), updateStatus = function() { if (x.state == 4 && x.responseCode == 200) notifyUser(i); } } My goal now is to e ...