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

JavaScript中JSON条目基础过滤失败问题求助

问题分析与修复

你遇到的ReferenceError: filter is not defined错误,核心是filter的调用方式和过滤逻辑都有问题:

错误点拆解

  1. filter调用方式错误:filter是数组的原型方法,不能直接独立调用,必须通过你获取到的JSON数组(也就是data)来调用,即data.filter(...)。
  2. 过滤逻辑写法错误:你直接写victime_type == "Vélo",但victime_type是数组中每个对象的属性,需要在回调函数中接收每一项元素,再访问其属性做判断。

修正后的代码

fetch(myRequest)
  .then((response) => response.json())
  // 修正filter调用方式与过滤逻辑
  .then((data) => data.filter(item => item.victime_type === "Vélo"))
  .then((filteredData) => process_per_gravite(filteredData, carte))
  .catch(console.error);

关键说明

  • data.filter(item => item.victime_type === "Vélo"):遍历data数组的每一项item,只保留victime_type属性等于"Vélo"的元素,最终返回过滤后的新数组。
  • 推荐使用严格相等运算符===替代==,避免隐式类型转换引发的潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 23:06:01