繁体   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

我正在使用县的选择来获得每个指定县的地区及其总人口。 但这并不适用于所有县,而是随机发生的。 我想从 select 选项中获取县,并返回该地区及其男性和女性的总人口。

这是代码

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

预期 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
        }
      }

//但是这个 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 }] selected_county.forEach(country => { population.forEach(el => { el.county == (country + " ") && console.log(el)}) })

运行代码时收到错误消息,但我认为您的问题是population中的县名末尾有空格。

我不确定您是否想要 object 中的全部人口,因为您预期的 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