简体   繁体   English

如何从多个嵌套对象中计算特定值的对象数?

[英]How do I count the number of objects a specific value from mutliple nested objects?

 var foodwebsites = { "bacon": [{ "url": "stackoverflow.com", }], "icecream": [{ "url": "example.com", }], "cheese": [{ "url": "example.com", }] } var baconfoodwebsites = foodwebsites.bacon.filter(function(elem) { return elem.url == 'example.com'; }).length; var icecreamfoodwebsites = foodwebsites.icecream.filter(function(elem) { return elem.url == 'example.com'; }).length; var cheesefoodwebsites = foodwebsites.cheese.filter(function(elem) { return elem.url == 'example.com'; }).length; var allfoodwebsites = baconfoodwebsites + icecreamfoodwebsites + cheesefoodwebsites; console.log(baconfoodwebsites, icecreamfoodwebsites, cheesefoodwebsites, allfoodwebsites) 

I'd like to do the exact same thing without the repetion of all these individual nested objects (bacon, icecream and cheese). 我想做同样的事情,而不必重复所有这些单独的嵌套对象(培根,冰淇淋和奶酪)。

I assume the answer would be like: 我认为答案将是:

var allfoodwebsites = foodwebsites=.filter(function( elem) {
    return elem.url == 'example.com';
}).length;

Additional Info: 附加信息:

I'd like to use only jQuery + Pure Javascript if possible. 如果可能的话,我只想使用jQuery + Pure Javascript。

I'd like to find all nested objects with "url": "example.com" 我想找到所有带有"url": "example.com"嵌套对象"url": "example.com"

The best option is the function reduce for counting approaches. 最好的选择是reduce计数方法的功能。

 let foodwebsites = {"bacon": [{"url": "stackoverflow.com",}],"icecream": [{"url": "example.com",}],"cheese": [{"url": "example.com",}]}; let allfoodwebsites = Object.values(foodwebsites). reduce((a, array) => a + array. reduce((a, {url}) => a + (url === "example.com"), 0), 0); console.log(allfoodwebsites); 

Using Object.values, Array#reduce() & Array#flat() . 使用Object.values,Array#reduce()和Array#flat()。 Note flat() may need polyfill in some environments 注意flat()在某些环境中可能需要polyfill

 let foodwebsites = {"bacon": [{"url": "stackoverflow.com",}],"icecream": [{"url": "example.com",}],"cheese": [{"url": "example.com",}]}; const getUrlCount = (url) => { return Object.values(foodwebsites) .flat() .reduce((a, {url:u})=> a + (url === u) , 0) } console.log(getUrlCount("example.com")) 

Solution: 解:

You can create a function that will iterate the object keys and search for a property and value inside each accessed key's array of child objects: 您可以创建一个函数,该函数将迭代对象键并在每个访问键的子对象数组中搜索propertyvalue

let amount = (p, v, i = 0) => 
(Object.keys(foodwebsites).forEach(k => foodwebsites[k].forEach(o => o[p] === v && i++))
, i);

and it can be used like this: 可以这样使用:

amount("url", "example.com"); // 2

Working Code Snippet: 工作代码段:

 var foodwebsites = { "bacon": [{ "url": "stackoverflow.com", }], "icecream": [{ "url": "example.com", }], "cheese": [{ "url": "example.com", }] } let amount = (p, v, i = 0) => (Object.keys(foodwebsites).forEach(k => foodwebsites[k].forEach(o => o[p] === v && i++)) , i); console.log( amount("url", "example.com") ); //2 

I'd actually built one array of all these nested entries: 我实际上建立了所有这些嵌套条目的一个数组:

  const sites = Object.values(foodwebsites).flat();

Then you can easily iterate over it and count all the keys: 然后,您可以轻松地对其进行迭代并计算所有键:

 const count = (arr, key, value) => arr.reduce((acc, it) => acc + it[key] === value, 0);

 console.log(
   count(sites, "url", "example.com"),
   count(sites, "url", "stackoverflow.com")
 );

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

相关问题 如何在嵌套的 JSON 对象中查找和计算唯一值? - How do i find and count unique values in nested JSON objects? 如何从 state 获取对象数组并将对象的特定值推送到新数组中 - How do I take an array of objects from state and push a specific value of the objects into a new array 如何在嵌套对象中搜索特定属性值并返回祖先元素? - How do I search nested objects for a specific property value and return an ancestor element? 如何在嵌套对象中找到具有特定属性的对象 - How do i find an object with specific property within nested objects 如何计算我创建的对象数量? - How to count number of objects I create? 计算具有特定属性值(JavaScript)的对象数组中的项目数 - Count number of items in an array of objects with a specific property value (JavaScript) 如何检查我推入对象的对象数组是否具有来自另一个对象数组的特定键值 - How to check if an array of objects where I push into objects has a specific key value from another array of objects 计算嵌套对象和 arrays 的键总数 - count the total number of keys from nested objects and arrays 如何在Javascript对象数组中搜索特定值? - How do I search for a specific value in an array of objects in Javascript? 如何从对象数组中动态获取特定数据? - How do I grab specific data dynamically from an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM