You need to enable JavaScript to run this app. 如何使用array.reduce或array.forEach按指定键值提取对象数组对应数据
优惠活动
大模型
产品
解决方案
定价
更多

如何使用array.reduce或array.forEach按指定键值提取对象数组对应数据

问题描述

我通过县选择器获取指定县对应的区县及其总人口,但该功能无法对所有县生效,仅随机对部分县生效。我的需求是:从选择选项中获取选中的县,返回对应区县的男性人口、女性人口及总人口数据。


现有代码

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

代码问题诊断

  1. 双重forEach逻辑错误:population本身就是对象数组,外层遍历拿到的ele已经是单个人口数据对象,不需要再对ele执行forEach
  2. 匹配逻辑错误:selected_county是数组,不能直接用===和单个县名字符串比较,需要用includes()方法判断当前县是否在选中列表中
  3. 字符匹配问题:population里的county字段末尾都带了空格,选中列表里的县名没有空格,直接匹配会失败
  4. 变量名拼写错误:定义的变量是district_population,赋值时写成了districts_population
  5. 输出结构不符合要求:需要按县分组,每个县下面挂载带总人口的区县列表,不是直接用区县名当key存储总人口

修复后代码

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
    }]
// 初始化结果对象
const result = {};
population.forEach(item => {
    // 去除县名首尾空格再匹配
    const countyName = item.county.trim();
    // 只处理选中的县
    if (selected_county.includes(countyName)) {
        // 如果当前县还没在结果里初始化,先赋值为空数组
        if (!result[countyName]) {
            result[countyName] = [];
        }
        // 组装带总人口的区县数据
        const districtData = {
            district: item.district,
            male: item.male,
            female: item.female,
            total: item.male + item.female
        };
        result[countyName].push(districtData);
    }
})
console.log(JSON.stringify(result, null, 2));

内容的提问来源于stack exchange,提问作者Benjamin A. Ngafua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 16:15:09