js或者es6 已知id为222的对象数据,如何查询并返回该数据中parentId: 7对应 ...

打印 上一主题 下一主题

主题 532|帖子 532|积分 1611

要查询并返回id为222的对象数据中parentId为7对应的对象的父节点数据,我们须要遍历整个data数组,找到id为222的对象,并从其父节点中提取信息。由于您提供的data数组中的对象格式存在问题(比方,对象的键值对应该用花括号{}包围),我将先帮您修正格式,然后提供查询的代码。
修正后的data数组格式如下:
  1. const data = [
  2.     {
  3.         "id": 1,
  4.         "name": "常用指标",
  5.         "parentId": 0,
  6.         "signalTypeIds": "",
  7.         "harmonicTimes": "",
  8.         "children": [
  9.             {
  10.                 "id": 7,
  11.                 "name": "相电压",
  12.                 "parentId": 1,
  13.                 "signalTypeIds": {
  14.                     "0": 1,
  15.                     "1": 94,
  16.                     "2": 161,
  17.                     "3": 228,
  18.                     "4": 295
  19.                 },
  20.                 "harmonicTimes": "",
  21.                 "children": [
  22.                     {
  23.                         "children": undefined,
  24.                         "harmonicTimes": "",
  25.                         "id": 222,
  26.                         "label": "A相",
  27.                         "name": "A相",
  28.                         "parentId": 7,
  29.                         "signalTypeIds": { "0": 1, "1": 94, "2": 161, "3": 228, "4": 295 },
  30.                         "value": "A相"
  31.                     }
  32.                 ]
  33.             }
  34.         ]
  35.     },
  36.     {
  37.         "id": 3,
  38.         "name": "电压电流功率",
  39.         "parentId": 0,
  40.         "signalTypeIds": "",
  41.         "harmonicTimes": "",
  42.         "children": [
  43.             {
  44.                 "id": 4,
  45.                 "name": "相电压",
  46.                 "parentId": 3,
  47.                 "signalTypeIds": {
  48.                     "0": 1,
  49.                     "1": 94,
  50.                     "2": 161,
  51.                     "3": 228,
  52.                     "4": 295
  53.                 },
  54.                 "harmonicTimes": "",
  55.                 "children": []
  56.             }
  57.         ]
  58.     }
  59. ];
复制代码
如今,我们可以使用以下递归函数来查询并返回id为222的对象的父节点数据:
  1. function findParentNodeById(id, data) {
  2.     for (const item of data) {
  3.         if (item.children) {
  4.             for (const child of item.children) {
  5.                 if (child.children) {
  6.                     // 递归搜索孙节点
  7.                     const result = findParentNodeById(id, child.children);
  8.                     if (result) return result; // 如果在孙节点中找到,返回结果
  9.                 } else if (child.id === id) {
  10.                     // 找到id为222的对象,返回其父节点
  11.                     return child;
  12.                 }
  13.             }
  14.         }
  15.     }
  16.     return null; // 如果没有找到,返回null
  17. }
  18. const parentNode = findParentNodeById(222, data);
  19. console.log(parentNode); // 这将输出id为222的对象的父节点数据
复制代码
这段代码界说了一个findParentNodeById函数,它担当两个参数:要搜索的id和要搜索的数组。函数内部,它遍历提供的数组及其全部子数组,查找id为指定值的对象。当找到id为222的对象时,它返回该对象的父节点数据。如果没有找到匹配的对象,函数返回null。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连全瓷种植牙齿制作中心

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表