epayne
epayne

Reputation: 149

Count total number of items in object based on property

Business =  {
  1: { Country: "France" },
  2: { Country: "France" },
  3: { Country: "UnitedKingdom" },
  4: { Country: "France" }
}

I have an object list of Businesses (assigned an ID), with each business having their own country.

My question is what is the best method in finding the amount via property? For example if I wanted to find the total amount of French businesses it would return 3 based on the code example.

Upvotes: 1

Views: 909

Answers (5)

Christos
Christos

Reputation: 53958

You could try something like this:

var Business = {
    1: {Country:"France"}
  , 2: {Country:"United Kingdom"}
  , 3: {Country:"France"}
  , 4: {Country:"France"}
};

var businessInFrance = Object.keys(Business)
                             .map(key => Business[key])
                             .reduce(function(acc, val){
                                 return val.Country === "France"
                                       ? acc+=1
                                       : acc;
                                 },0);
                        
console.log(businessInFrance);

  • Object.keys(Business) returns an array with the enumerable that Business owns
  • map applies the function we pass to the output of the previous step and returns a new array. The element of this array are the value of the Business
  • reduce applies the function we pass for each element of the array of the previous step and when the Country property has a value equal to "France", we increment by one the value of the accumulator, acc, otherwise we return it unchanged. At then end the value of accumulator is returned.

Upvotes: 2

Farooq Ahmed Khan
Farooq Ahmed Khan

Reputation: 4094

Object.keys returns the keys of object. You can loop over the keys and count your desired result. Here is an example..

var Business =  {
1:{
  Country: "France"
},
2:{
  Country: "France"
},
3:{
  Country: "UnitedKingdom"
},
4:{
  Country: "France"
}};

var countByCountry = function(country){
  var result = 0;
  Object.keys(Business).forEach(function(it){
    result += (Business[it].Country === country ? 1 : 0);
    console.log(it, country, result);
  });
  return result;
}

console.log(countByCountry('France'));

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386883

You could use an object for counting countries.

var business = { 1: { Country: "France" }, 2: { Country: "France" }, 3: { Country: "UnitedKingdom" }, 4: { Country: "France" } },
    count = Object.keys(business).reduce(function (r, k) {
        var country = business[k].Country;
        r[country] = (r[country] || 0) + 1;
        return r;
    }, Object.create(null));

console.log(count.France);
console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

synthet1c
synthet1c

Reputation: 6282

You have an issue with your object. You could use the following, but I have to ask why not just use an array?

Business = {
  01: {Country:"France"},
  02: {Country:"United Kingdom"},
  03: {Country:"France"},
  04: {Country:"France"},
}

const BusinessArray = [
  {Country:"France"},
  {Country:"United Kingdom"},
  {Country:"France"},
  {Country:"France"},
]

console.log(
  BusinessArray.filter(x => x.Country === 'France').length
)

Business = {
  01: {Country:"France"},
  02: {Country:"United Kingdom"},
  03: {Country:"France"},
  04: {Country:"France"},
}

const countriesCount = (country, businesses) => 
  Object.keys(businesses)
    .filter(x => businesses[x].Country === country)
    .length
    
console.log(
  countriesCount('France', Business),
  countriesCount('United Kingdom', Business)
)

Upvotes: 1

Scott Marcus
Scott Marcus

Reputation: 65883

First, your syntax is incorrect and will lead to an error.

If you contain all the objects in an array, you wouldn't need the objects to maintain their own identifier and you can do this very easily:

var business = [
   {country:"France"},
   {country:"United Kingdom"},
   {country:"France"},
   {country:"France"}
];


console.log("The amount of countries is: " +  business.length);

var count = 0;
business.forEach(function(value){
   count = (value.country === "France") ? ++count : count;
});

console.log("The amount of times France appears is: " + count);

Upvotes: 0

Related Questions