简体   繁体   中英

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.

I'd like to find all nested objects with "url": "example.com"

The best option is the function reduce for counting approaches.

 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() . Note flat() may need polyfill in some environments

 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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