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

如何用JSON Query筛选含type:floor属性的对象id列表

提取attributes.type为"floor"的对象ID列表解决方案

1. JSONPath语法(支持JSONPath的工具/库适用)

直接用以下表达式即可获取目标ID数组:

$..[?(@.attributes.type == 'floor')].id

语法说明:

  • $..:递归遍历所有嵌套层级的对象
  • [?(@.attributes.type == 'floor')]:筛选出attributes.type等于"floor"的对象
  • .id:提取这些对象的id属性

比如针对示例嵌套JSON,执行后会返回:["floor_1", "floor_2"]

2. JavaScript/TypeScript代码实现(前端/Node.js场景)

用数组的filter+map组合直接处理:

// 假设原始数据存储在rawData变量中
const rawData = {
  "data": [
    { "id": "floor_1", "attributes": { "type": "floor" } },
    { "id": "room_1", "attributes": { "type": "room" } },
    { "id": "floor_2", "attributes": { "type": "floor" } }
  ]
};

const floorIds = rawData.data
  .filter(item => item.attributes?.type === 'floor')
  .map(item => item.id);

console.log(floorIds); // 输出 ["floor_1", "floor_2"]
  • 用filter筛选符合条件的对象,可选链?.避免属性不存在时抛出错误
  • 用map提取筛选后对象的id值,生成纯ID数组

3. 针对已有中间结果的优化

如果之前的查询已经得到了包含ID和类型的结构(比如[{id: 'xxx', type: 'floor'}, ...]),直接对结果做map提取ID即可:

// 假设中间结果存储在result变量中
const floorIds = result.map(item => item.id);

内容的提问来源于stack exchange,提问作者afranco.pt

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 02:59:55