[英]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"))
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: 您可以创建一个函数,该函数将迭代对象键并在每个访问键的子对象数组中搜索
property
和value
:
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
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.