Split out array and counting in array

Hi Expert,

it is a simple question for you but not me, i list 6 items from airtable record, here below are the array record, and i would like to extract one field named “who_win_hazlo” (it is sits in “fields”)

for the “who_win”, there is only 3 results “Home”, “Draw”, “Away”.
And i would like to count the result how many “Home”, how many “Draw” and how many “Away”…

for the results, there should be count 3 for “Home”, count 2 for “Draw”, 1 for “Away”

i tried few hours but have no idea, can you please share some ideas??? thanks

[
  {
    "id": "rec2vEVBSXmPsqIUQ",
    "createdTime": "2022-08-10T18:36:31.000Z",
    "fields": {
      "URL": "http://op1.titan007.com/Oddslist/2223547_2.htm",
      "Home_team_r": "AAA",
      "away_team": "BBB",
      "home_score": 2,
      "away_score": 0,
      "who_win": 3,
      "who_win_hazlo": "Home"
    }
  },
 {
    "id": "rec2vEVBSXmPsqIUa",
    "createdTime": "2022-08-10T18:36:31.000Z",
    "fields": {
      "URL": "http://op1.titan007.com/Oddslist/2223547_2.htm",
      "Home_team_r": "CCC",
      "away_team": "DDD",
      "home_score": 2,
      "away_score": 0,
      "who_win": 1,
      "who_win_hazlo": "Draw"
    }
  },
{
    "id": "rec2vEVBSXmPsqIU1",
    "createdTime": "2022-08-10T18:36:31.000Z",
    "fields": {
      "URL": "http://op1.titan007.com/Oddslist/2223547_2.htm",
      "Home_team_r": "EEE",
      "away_team": "FFF",
      "home_score": 2,
      "away_score": 0,
      "who_win": 3,
      "who_win_hazlo": "Home"
    }
  },
{
    "id": "rec2vEVBSXmPsqIU3",
    "createdTime": "2022-08-10T18:36:31.000Z",
    "fields": {
      "URL": "http://op1.titan007.com/Oddslist/2223547_2.htm",
      "Home_team_r": "GGG",
      "away_team": "HHH",
      "home_score": 2,
      "away_score": 0,
      "who_win": 1,
      "who_win_hazlo": "Draw"
    }
  },
{
    "id": "rec2vEVBSXmPsqIU6",
    "createdTime": "2022-08-10T18:36:31.000Z",
    "fields": {
      "URL": "http://op1.titan007.com/Oddslist/2223547_2.htm",
      "Home_team_r": "III",
      "away_team": "JJJ",
      "home_score": 2,
      "away_score": 0,
      "who_win": 0,
      "who_win_hazlo": "Away"
    }
  },
{
    "id": "rec2vEVBSXmPsqIUQ",
    "createdTime": "2022-08-10T18:36:31.000Z",
    "fields": {
      "URL": "http://op1.titan007.com/Oddslist/2223547_2.htm",
      "Home_team_r": "KKK",
      "away_team": "LLL",
      "home_score": 2,
      "away_score": 0,
      "who_win": 3,
      "who_win_hazlo": "Home"
    }
  }
]

Hi @Louis_kwok, I think you can approach this similar to what I’ve described earlier in Count occurrences of each item in JSON - #15 by MutedJam, just a bit simpler.

Here’s an example using your data:

Returning this result:

Is this what you were looking for?

If you’re using an older version of n8n (< 0.166.0) you might need to wrap the result in the required data structure, but for up to date versions it should work out of the box without having to make changes.

1 Like

hi friend, it works, you save my time, really thanks!!

1 Like

Glad to hear, thanks for confirming :slight_smile:

hi Jam, one more follow up question, sometimes the result may only has “Home” or “Away”, without “Draw”,

so i would like to have a simple if-else statement for the express node.
for example if “Draw” is empty (return no result), then show 0,

who can i set this expression?? thanks

Ah, so you’d always like to include fields Home, Away, and Draw, even when they’re not present in the source dataset? In this case you’d need to hard code them (as n8n would otherwise not know about them), for example like so:

Now even if one (or more) of the values is 0, the respective field will be part of the result:

1 Like