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

合并两个axios.get请求结果并过滤,解决仅显示首个请求数据问题

问题解决方法

你的代码问题在于没有正确合并两次请求的数组,而是错误地将第二个请求的数组作为filter方法的第二个参数传入——但filter的第二个参数是用来指定回调函数的this指向,并非要处理的数据源,所以第二个请求的数据完全没被纳入处理流程。

修正步骤:

  1. 先合并两次请求返回的entries数组
  2. 再对合并后的数组统一执行过滤逻辑

修正后的代码:

const firstRequest = await axios.get(`${fox.url}/nodes/${firstFolderId}/children`, PARAMS);
const secondRequest = await axios.get(`${fox.url}/nodes/${secondFolderId}/children`, PARAMS);

// 合并两个请求的entries数组
const combinedEntries = [...firstRequest.data.list.entries, ...secondRequest.data.list.entries];

// 对合并后的数组执行过滤(可合并两个filter为一个,提升效率)
const types = _(combinedEntries)
  .filter(type => !type.entry.name.includes('dog') && !type.entry.name.includes('cat'))
  .value();

const resourcesTypes = getParserTypes(types, subject);

console.log("resourcesTypes", resourcesTypes);

补充说明:

  • 用扩展运算符[...arr1, ...arr2]是最直观的数组合并方式,也可以用lodash的_.concat(arr1, arr2)实现
  • 把两个filter合并成一个条件判断,减少一次数组遍历,性能更优

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 23:10:30