简体   繁体   English

有没有一种最佳方法可以根据其中一个键的值从 object 数组中返回所有值。 使用 array.reduce 或 array.forEach

[英]Is there a best way to return all values from an array of object based on the value of one of the keys. Using array.reduce or array.forEach

I am use a selection on counties to get the district and its total population per the county specified.我正在使用县的选择来获得每个指定县的地区及其总人口。 But this is not working for all the counties rather it's happening just randomly.但这并不适用于所有县,而是随机发生的。 I want to get the county from the select option and return the district and its total population for male and female.我想从 select 选项中获取县,并返回该地区及其男性和女性的总人口。

This is the code这是代码

let selected_county = ["Lofa", "Bomi", "Bong", "River Gee", "Sinoe"];
let population =[
         { "county": "River Gee ",
            "district": "Glaro ",
            "male": 2613,
            "female": 2379
        },
        {
            "county": "River Gee ",
            "district": "Karforh ",
            "male": 3072,
            "female": 2884
        },
        {
            "county": "Lofa ",
            "district": "Nanee ",
            "male": 3421,
            "female": 2581
        },
        {
            "county": "Lofa ",
            "district": "Nyenawliken ",
            "male": 2668,
            "female": 2491
        },
        {
            "county": "Bong ",
            "district": "Nyenebo ",
            "male": 3046,
            "female": 2657
        },
        {
            "county": "Bong ",
            "district": "Potupo",
            "male": 3689,
            "female": 3648
        },
        {
            "county": "Bomi ",
            "district": "Sarbo ",
            "male": 2680,
            "female": 2640
        },
        {
            "county": "Bomi ",
            "district": "Tuobo ",
            "male": 2496,
            "female": 2372
        },
        {
            "county": "Sinoe ",
            "district": "Bodae ",
            "male": 2151,
            "female": 1388
        },
        {
            "county": "Sinoe ",
            "district": "Bokon ",
            "male": 2457,
            "female": 1916
        }]
   let district_population = { };

   population.forEach((ele)=>{
       ele.forEach((elem) => {
         if (elem.county === selected_county) { 

    //selected_county has the array of county used in the population's array

           districts_population[elem.district] = elem.male + elem.female;
   
           }
        })
    })
   console.log(districts_population);

Expected output:预期 output:

"River Gee" : {{
            "district": "Karforh ",
            "male": 3072,
            "female": 2884,
             "total" : 5956
            },
            {
             "district": "Karforh ",
            "male": 3072,
            "female": 2884,
              "total": 5956
            }}
    "Lofa" :{
            {
            "district": "Nanee ",
            "male": 3421,
            "female": 2581,
             "total" : 6002
        },
        {
          "district": "Nyenawliken ",
            "male": 2668,
            "female": 2491,
            "total": 5159
        }
       }
"Bong": {{
            "district": "Nyenebo ",
            "male": 3046,
            "female": 2657,
            "total": 5159
        },
        { 
            "district": "Potupo",
            "male": 3689,
            "female": 3648,
            "total": 5159
        }
      }
"Bomi":{{
            "district": "Sarbo ",
            "male": 2680,
            "female": 2640,
            "total": 5159
        },
        {
            "district": "Tuobo ",
            "male": 2496,
            "female": 2372,
            "total": 5159
        }
      }
"Sinoe": {{ 
            "district": "Bodae ",
            "male": 2151,
            "female": 1388
            "female": 2372,
            "total": 5159
        },
        { 
            "district": "Bokon ",
            "male": 2457,
            "female": 1916
            "female": 2372,
            "total": 5159
        }
      }

//But this output is only happening for few counties not all //但是这个 output 只发生在少数几个县不是全部

Looks like there's a space after each county name, which means it won't match.看起来每个县名后面都有一个空格,这意味着它不匹配。 Here's a quick solution...这是一个快速的解决方案...

 let selected_county = ["Lofa", "Bomi", "Bong", "River Gee", "Sinoe"]; let population =[ { "county": "River Gee ", "district": "Glaro ", "male": 2613, "female": 2379 }, { "county": "River Gee ", "district": "Karforh ", "male": 3072, "female": 2884 }, { "county": "Lofa ", "district": "Nanee ", "male": 3421, "female": 2581 }, { "county": "Lofa ", "district": "Nyenawliken ", "male": 2668, "female": 2491 }, { "county": "Bong ", "district": "Nyenebo ", "male": 3046, "female": 2657 }, { "county": "Bong ", "district": "Potupo", "male": 3689, "female": 3648 }, { "county": "Bomi ", "district": "Sarbo ", "male": 2680, "female": 2640 }, { "county": "Bomi ", "district": "Tuobo ", "male": 2496, "female": 2372 }, { "county": "Sinoe ", "district": "Bodae ", "male": 2151, "female": 1388 }, { "county": "Sinoe ", "district": "Bokon ", "male": 2457, "female": 1916 }] selected_county.forEach(country => { population.forEach(el => { el.county == (country + " ") && console.log(el)}) })

I receive an error while running the code, but I think your problem is that there are spaces at the end of the county names in population .运行代码时收到错误消息,但我认为您的问题是population中的县名末尾有空格。

I'm not sure whether you want the full population, in an object, because your expected output is different.我不确定您是否想要 object 中的全部人口,因为您预期的 output 是不同的。 I just went with your expected output, but if you clear it up, I can edit my answer, to the correct output我刚刚使用了您预期的 output,但是如果您清除它,我可以将我的答案编辑为正确的 output

 let selected_county = ["Lofa", "Bomi", "Bong", "River Gee", "Sinoe"]; let population =[ { "county": "River Gee", "district": "Glaro ", "male": 2613, "female": 2379 }, { "county": "River Gee", "district": "Karforh ", "male": 3072, "female": 2884 }, { "county": "Lofa ", "district": "Nanee ", "male": 3421, "female": 2581 }, { "county": "Lofa ", "district": "Nyenawliken ", "male": 2668, "female": 2491 }, { "county": "Bong ", "district": "Nyenebo ", "male": 3046, "female": 2657 }, { "county": "Bong ", "district": "Potupo", "male": 3689, "female": 3648 }, { "county": "Bomi ", "district": "Sarbo ", "male": 2680, "female": 2640 }, { "county": "Bomi ", "district": "Tuobo ", "male": 2496, "female": 2372 }, { "county": "Sinoe ", "district": "Bodae ", "male": 2151, "female": 1388 }, { "county": "Sinoe ", "district": "Bokon ", "male": 2457, "female": 1916 }] let districts_population=population.filter(count=>selected_county.includes(count.county.trim())) console.log(districts_population);

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

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