简体   繁体   English

根据属性计算对象中的项目总数

[英]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. 我有一个企业对象列表(分配了一个ID),每个企业都有自己的国家。

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. 例如,如果我想查找法国企业的总数,则根据代码示例将返回3。

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 Object.keys(Business)返回一个数组,其中包含Business拥有的枚举
  • map applies the function we pass to the output of the previous step and returns a new array. map将传递给上一步的函数应用到函数,并返回一个新数组。 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. reduce对上一步数组的每个元素应用传递的函数,当Country属性的值等于“ France”时,我们将累加器acc的值加1,否则将其返回不变。 At then end the value of accumulator is returned. 然后,返回累加器的值。

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) ) 

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; } 

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); 

Object.keys returns the keys of object. Object.keys返回对象的键。 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')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM