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

如何使用Javascript过滤Airtable返回的嵌套JSON数组数据

错误原因

  1. 第一种过滤写法的问题:Airtable返回的业务字段(product_name、price、highlight、prod_id等)都存放在每条记录的fields属性下,直接访问d.product_name读取不到对应值,需要改为d.fields.product_name
  2. 第二种过滤写法的问题:JS存在自动分号插入机制,return单独占一行时会被自动插入分号,相当于直接返回undefined,过滤条件完全未生效,需要把判断逻辑和return写在同一行,或者用大括号包裹逻辑块。

正确过滤示例

单条件过滤(按product_name匹配)

const output_data = result.records.filter(d => d.fields.product_name === "product 1");

单条件过滤(按price匹配)

const output_data = result.records.filter(d => d.fields.price === 35);

按根层级id过滤

id、createdTime是直接挂载在记录对象下的属性,不需要走fields读取:

const output_data = result.records.filter(d => d.id === "reck1GtJ3KfhW9zEO");

多条件组合过滤

示例:筛选price大于20、highlight为true、prod_id为G002的记录:

const output_data = result.records.filter(d => {
  return d.fields.price > 20 && d.fields.highlight === true && d.fields.prod_id === "G002"
});

通用多字段组合过滤工具函数

如果需要频繁切换过滤条件,可以封装为通用工具,传入条件对象即可自动匹配:

function filterAirtableRecords(records, conditions) {
  return records.filter(record => {
    return Object.entries(conditions).every(([key, value]) => {
      // 优先匹配根层级字段(id/createdTime),没有则从fields中读取业务字段
      const targetValue = record.hasOwnProperty(key) ? record[key] : record.fields[key];
      return targetValue === value;
    })
  })
}

// 使用示例1:筛选product_name为product 4、price为35的记录
const output_data = filterAirtableRecords(result.records, {
  product_name: "product 4",
  price: 35
})

// 使用示例2:按id筛选记录
const output_data2 = filterAirtableRecords(result.records, {
  id: "recV39iR6tBzrmZ5s"
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 06:48:05