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

如何从嵌套对象数组中提取所有层级的唯一refIds

提取嵌套对象数组中所有唯一的refIds

问题说明

需要从嵌套对象数组里提取所有层级的唯一refIds,但数据嵌套深度不确定,用普通for循环实现复杂度高。数据中必然存在"type": "text"的元素,这类元素可能带可选的"marks"字段,目标refIds就藏在type为"refId"的标记中。

示例数据

[
  {
    "type": "bulletList",
    "content": [
      {
        "type": "listItem",
        "content": [
          {
            "type": "paragraph",
            "content": [
              {
                "type": "text",
                "marks": [ // 可选标记
                  { "type": "refId", "attrs": { "refIds": [123, 234] } } // 需要提取的值
                ],
                "text": "Item 1"
              }
            ]
          }
        ]
      },
      {
        "type": "listItem",
        "content": [
          {
            "type": "paragraph",
            "content": [
              {
                "type": "text",
                "marks": [{ "type": "refId", "attrs": { "refIds": [987] } }],
                "text": "Item 2"
              }
            ]
          }
        ]
      },
      {
        "type": "listItem",
        "content": [
          {
            "type": "paragraph",
            "content": [{ "type": "text", "text": "Item 3" }] // 无标记
          },
          {
            "type": "bulletList", // 嵌套子元素示例
            "content": [
              {
                "type": "listItem",
                "content": [
                  {
                    "type": "paragraph",
                    "content": [
                      {
                        "type": "text",
                        "marks": [
                          { "type": "refId", "attrs": { "refIds": [876] } }
                        ],
                        "text": "Sub 1"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

预期输出

["123", "234", "876", "987"]

解决方案

用递归遍历处理任意深度的嵌套结构,结合Set自动去重,最终得到目标数组:

function extractUniqueRefIds(data) {
  const refIds = new Set();

  // 递归遍历函数
  function traverse(item) {
    // 处理text类型元素,提取refIds
    if (item.type === 'text') {
      if (item.marks) {
        item.marks.forEach(mark => {
          if (mark.type === 'refId') {
            mark.attrs.refIds.forEach(id => {
              refIds.add(id.toString()); // 转为字符串匹配预期输出格式
            });
          }
        });
      }
    }
    // 递归处理子元素(如果有content字段)
    if (item.content && Array.isArray(item.content)) {
      item.content.forEach(child => traverse(child));
    }
  }

  // 遍历输入数组的每个元素
  data.forEach(item => traverse(item));

  // 将Set转为数组返回
  return Array.from(refIds);
}

// 测试示例数据
const exampleData = [/* 上述示例数据 */];
console.log(extractUniqueRefIds(exampleData));

思路说明

  1. 递归遍历:不管嵌套深度多少,递归函数会自动进入每个含content的元素,遍历所有层级的子元素。
  2. 目标元素处理:遇到type为text的元素时,检查是否有marks字段,筛选出type为refId的标记,提取其中的refIds。
  3. 去重处理:用Set存储提取到的id,自动实现去重,最后转成数组即可得到结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:57:51