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

如何修改JS函数返回匹配搜索列表的嵌套子对象而非父级

问题解决:从嵌套对象中返回匹配键的子对象

需求说明

实现JavaScript函数,从给定嵌套数据结构中返回与搜索列表匹配键对应的子对象(而非匹配键自身的对象)。当前函数返回匹配键的对象,需修改以达成目标。

测试数据

var data = { 
  "tabs-1": { 
    "test 1": { 
      "test 2": { 
        "test 3a": { 
          "tab1graph1": { 
            "String a": "value a", 
            "String b": "value b", 
            "String c": "value c" 
          } 
        }, 
        "test 3b": { 
          "tab1graph2": { 
            "String a": "value a", 
            "String b": "value b", 
            "String c": "value c" 
          } 
        }, 
        "test 3c": { 
          "tab1graph3": { 
            "String a": "value a", 
            "String b": "value b", 
            "String c": "value c" 
          } 
        } 
      } 
    } 
  } 
};

搜索输入

var searchList = ["apple", "orange", "test 3b", "test 3a"];

当前代码(存在问题)

function searchObjectForValues(obj, searchList) { 
  var matchingObjects = {}; 

  function recursiveSearch(currentObj, path) { 
    if (typeof currentObj === 'object' && currentObj !== null) { 
      for (var key in currentObj) { 
        if (currentObj.hasOwnProperty(key)) { 
          var value = currentObj[key]; 
          var currentPath = path.concat(key); 

          if (searchList.includes(key)) { 
            matchingObjects[key] = currentObj[key]; 
          } 

          recursiveSearch(value, currentPath); 
        } 
      } 
    } 
  } 

  recursiveSearch(obj, []); 

  return matchingObjects; 
}

问题分析

当前代码匹配到searchList中的键时,直接将该键对应的对象存入结果,但需求是要获取该对象的子对象(比如匹配到"test 3a"时,需返回其下的"tab1graph1"对象)。

修改后的代码

function searchObjectForValues(obj, searchList) { 
  var matchingObjects = {}; 

  function recursiveSearch(currentObj, path) { 
    if (typeof currentObj === 'object' && currentObj !== null) { 
      for (var key in currentObj) { 
        if (currentObj.hasOwnProperty(key)) { 
          var value = currentObj[key]; 
          var currentPath = path.concat(key); 

          if (searchList.includes(key)) {
            // 遍历匹配键的子对象,将子项存入结果
            for (var childKey in value) {
              if (value.hasOwnProperty(childKey)) {
                // 保留原匹配键作为结果的键,关联对应的子对象
                matchingObjects[key] = value[childKey];
                // 若匹配键下有多个子对象,改为数组存储:
                // matchingObjects[key] = matchingObjects[key] || [];
                // matchingObjects[key].push(value[childKey]);
              }
            }
          } 

          recursiveSearch(value, currentPath); 
        } 
      } 
    } 
  } 

  recursiveSearch(obj, []); 

  return matchingObjects; 
}

改动说明

  1. 匹配到目标键时,不再直接存储键对应的对象,而是遍历该对象的子属性,将子对象存入结果。
  2. 默认适配每个匹配键下仅有一个子对象的场景,若存在多个子对象,可按注释修改为数组存储。
  3. 运行修改后的函数,给定测试数据和搜索输入会返回:
{
  "test 3a": { "String a": "value a", "String b": "value b", "String c": "value c" },
  "test 3b": { "String a": "value a", "String b": "value b", "String c": "value c" }
}

若想要用子对象的键作为结果的键,可将matchingObjects[key] = value[childKey]改为matchingObjects[childKey] = value[childKey],得到:

{
  "tab1graph1": { "String a": "value a", "String b": "value b", "String c": "value c" },
  "tab1graph2": { "String a": "value a", "String b": "value b", "String c": "value c" }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 21:22:47