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

React Native中如何从数组中仅提取指定分类的对应数据

提取指定分类数据的实现方案

核心逻辑

你的接口返回数据中,所有分类的完整信息都存储在responseContents数组中,仅需过滤该数组内Categories字段和目标分类名一致的元素,即可拿到对应分类的全部数据。

常见语言实现示例

JavaScript 代码

// 接口返回的完整JSON数据存入apiResult变量
const targetCategory = "Tools & Machineries";
// 筛选匹配的分类数据,返回数组(支持同分类名多条数据)
const matchedResult = apiResult.responseContents.filter(item => item.Categories === targetCategory);

// 如果确认分类名全局唯一,可直接用find取单个对象
// const matchedResult = apiResult.responseContents.find(item => item.Categories === targetCategory);

console.log(matchedResult);

Python 代码

import json

# 先解析接口返回的JSON字符串为字典
api_result = json.loads(你的接口返回JSON字符串)
target_category = "Tools & Machineries"
# 列表推导式筛选匹配项,返回列表
matched_result = [item for item in api_result["responseContents"] if item["Categories"] == target_category]

# 如果分类名唯一,直接取单个对象
# matched_result = next(item for item in api_result["responseContents"] if item["Categories"] == target_category)

print(matched_result)

优化提示

如果匹配时出现匹配失败的情况,建议先对字段做空格、大小写统一处理后再比较,示例如下(以JS为例):

const matchedResult = apiResult.responseContents.filter(item => 
  item.Categories.trim().toLowerCase() === targetCategory.trim().toLowerCase()
);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 21:27:03