You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用字符串数组过滤对象数组?筛选含关键词且数值>0的项

种族族群数据过滤函数实现

问题背景

现有如下格式的data数组:

const data = [
 {
  groups: [
      {
          "raceEthnicity": {
              "Black or African American": 45,
              "American Indian or Alaska Native": 18,
              "Asian - - East Asian": 27,
              "White - Arabic/North African": 7,
              "White - White/Caucasian/European": 142,
              "Mixed/Other/Unspecified": 3
          }
      },
      {
          "raceEthnicity": {
              "Black or African American": 48,
              "American Indian or Alaska Native": 14,
              "Asian - East Asian": 36,
              "Asian - South East Asian": 3,
              "White - Arabic/North African": 4,
              "White - White/Caucasian/European": 140,
              "Mixed/Other/Unspecified": 2
          }
      }
  ]
 },
 {
  "groups": [
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 11,
              "Black or African American": 47,
              "Asian - South East Asian": 0,
              "White - Arabic/North African": 1,
              "White - White/Caucasian/European": 9,
              "Mixed/Other/Unspecified": 1
          },
      },
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 4,
              "Black or African American": 28,
              "Asian - South East Asian": 2,
              "White - Arabic/North African": 0,
              "White - White/Caucasian/European": 9,
              "Mixed/Other/Unspecified": 1
          },
      }
  ]
 },
 {
  "groups": [
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 11,
              "Black or African American": 47,
              "Asian - South East Asian": 0,
              "White - Arabic/North African": 1,
              "White - White/Caucasian/European": 9,
              "Mixed/Other/Unspecified": 1
          },
      },
      {
          "raceEthnicity": {
              "American Indian or Alaska Native": 4,
              "Black or African American": 28,
              "Asian - South East Asian": 2,
              "White": 20,
              "Mixed/Other/Unspecified": 1
          },
      }
  ]
 }
]

需要编写一个过滤函数,接收类似["Asian", "White"]的字符串数组作为参数,返回所有满足条件的对象:该对象的raceEthnicity属性中,包含指定关键词的键对应的数值大于0(例如键"Asian - East Asian"属于Asian群体,若其数值>0则该对象符合条件)。

解决方案

方案1:返回符合条件的Group对象

如果需要直接返回groups数组中满足条件的单个对象,使用以下函数:

function filterRaceGroups(data, keywords) {
  const matchingGroups = [];
  
  data.forEach(dataItem => {
    dataItem.groups.forEach(group => {
      const raceData = group.raceEthnicity;
      // 检查当前group是否匹配任意关键词的条件
      const isMatch = keywords.some(keyword => {
        return Object.entries(raceData).some(([raceKey, count]) => {
          return raceKey.includes(keyword) && count > 0;
        });
      });
      
      if (isMatch) {
        matchingGroups.push(group);
      }
    });
  });
  
  return matchingGroups;
}

方案2:返回包含符合条件Group的外层Data对象

如果需要返回整个外层data数组中的对象(只要该对象的groups里有至少一个符合条件的group),使用这个版本:

function filterDataItems(data, keywords) {
  return data.filter(dataItem => {
    // 检查当前data项是否有至少一个符合条件的group
    return dataItem.groups.some(group => {
      const raceData = group.raceEthnicity;
      return keywords.some(keyword => {
        return Object.entries(raceData).some(([raceKey, count]) => {
          return raceKey.includes(keyword) && count > 0;
        });
      });
    });
  });
}

使用示例

// 获取所有符合Asian或White条件的group对象
const resultGroups = filterRaceGroups(data, ["Asian", "White"]);
console.log(resultGroups);

// 获取所有包含符合条件group的外层data对象
const resultDataItems = filterDataItems(data, ["Asian", "White"]);
console.log(resultDataItems);

逻辑说明

  • keywords.some(keyword):只要有一个关键词满足条件,当前对象就符合要求
  • Object.entries(raceEthnicity).some(...):遍历种族族群的键值对,检查是否存在包含目标关键词的键,且对应数值大于0
  • 多层遍历确保不会遗漏任何符合条件的对象

内容的提问来源于stack exchange,提问作者DenisPaul

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 04:25:10