0

My question deals with the following JSON data :

{"matches":[
  {
    "country":"USA", 
    "postcode":"9011"
  },
  {
    "country":"USA", 
    "postcode":"9010"
  },
  {
    "country":"UK", 
    "postcode":"BB3"
  }
]}

Could anyone tell me how to retrieve the amount of times where country = USA ?

In the current case, the desired output is : 2.

I have been searching for hours how to do this, but have been unable to find the solution.

Thanks in advance for your help.

Regards,

GDC
  • 11
  • 1
  • Possible duplicate of [Counting how many times a value of a certain key appears in JSON](https://stackoverflow.com/questions/43480949/counting-how-many-times-a-value-of-a-certain-key-appears-in-json) or [Count property values of javascript object](//stackoverflow.com/q/17615364) – Heretic Monkey Aug 06 '18 at 01:36

5 Answers5

1

Just loop through and count. You can use reduce() for this and increment the count when the value matches what you want.

let o = {"matches":[{"country":"USA", "postcode":"9011"},{"country":"USA", "postcode":"9010"},{"country":"UK", "postcode":"BB3"}]}

let num_usa = o.matches.reduce((count, el) => {
  if (el.country === 'USA') count++
  return count
}, 0)
console.log(num_usa)
Mark
  • 90,562
  • 7
  • 108
  • 148
0

I thank the users that have replied for the help they have provided.

For some reason, I could process the JSON when it was written in a variable (as in the replies), but not when the same JSON was returned from an ajax process.

My issue has been finally solved by switching to XML.

<matches>
    <result country = "USA" postcode = "9011" />
    <result country = "USA" postcode = "9011" />
    <result country = "UK" postcode = "BB3" />
</matches>

var countcountry = $(xml_result).find('result[country="usa"]').length;

Returns : 2

GDC
  • 11
  • 1
0

using reduce is definitely my suggestion. In case you're interested to count other country, you can do it this way below:

const response = {
  "matches": [{
    "country": "USA",
    "postcode": "9011"
  }, {
    "country": "USA",
    "postcode": "9010"
  }, {
    "country": "UK",
    "postcode": "BB3"
  }]
}

const countryCount = response.matches.reduce((acc, match) => {
  const country = match.country;
  if (!acc[country]) {
    acc[country] = 1;
  } else {
    acc[country]++;
  }
  return acc;
}, {});

// print USA and UK
console.log(countryCount.USA);
console.log(countryCount.UK);
deerawan
  • 8,002
  • 5
  • 42
  • 51
0

Try this :

var jsonObj = {"matches":[
  {
    "country":"USA", 
    "postcode":"9011"
  },
  {
    "country":"USA", 
    "postcode":"9010"
  },
  {
    "country":"UK", 
    "postcode":"BB3"
  }
]};

var count = 0;

for (var i in jsonObj.matches) {
   (jsonObj.matches[i].country == 'USA') ? count = count+1 : 0;  
}

console.log("Country count with USA :", count);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
-1

Using Array#filter() and length of resultant is fairly simple for this use case

let data = {"matches":[{"country":"USA", "postcode":"9011"},{"country":"USA", "postcode":"9010"},{"country":"UK", "postcode":"BB3"}]}

let us_count = data.matches.filter(({country:c}) => c === 'USA').length

console.log(us_count)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • More efficient to use `reduce` since you're trying to get a single value, and `filter` makes a copy of all those references. – Brad Aug 06 '18 at 03:55