使用JS获得层次数据中的ID

发布于 2025-02-11 21:43:53 字数 792 浏览 2 评论 0原文

我将层次数据作为树数组:

var myData = [
    {
      id: 0,
      title:"Item 1"
    }, {
      id: 1,
      title:"Item 2",
      subs: [
        {
          id: 10,
          title:"Item 2-1",
          subs: [
        {
          id: 100,
          title:"Item 2-2-1"
        }, {
          id: 110,
          title:"Item 2-2-2"
        }, {
          id: 120,
          title:"Item 2-2-3"
        }
      ]
        }, {
          id: 11,
          title:"Item 2-2"
        }, {
          id: 12,
          title:"Item 2-3"
        }
      ]
    }, {
      id: 2,
      title:"Item 3"
    },
    // more data here
];

我需要在此数组中获得标题的ID。我尝试使用此功能:

console.log(myData.findIndex(item=>item.title==="Item 3"))

但是它对“项目2-2”有害。我应该如何解决这个问题?

I have hierarcial data as tree array:

var myData = [
    {
      id: 0,
      title:"Item 1"
    }, {
      id: 1,
      title:"Item 2",
      subs: [
        {
          id: 10,
          title:"Item 2-1",
          subs: [
        {
          id: 100,
          title:"Item 2-2-1"
        }, {
          id: 110,
          title:"Item 2-2-2"
        }, {
          id: 120,
          title:"Item 2-2-3"
        }
      ]
        }, {
          id: 11,
          title:"Item 2-2"
        }, {
          id: 12,
          title:"Item 2-3"
        }
      ]
    }, {
      id: 2,
      title:"Item 3"
    },
    // more data here
];

I need to get id by title in this array. I try to use this function:

console.log(myData.findIndex(item=>item.title==="Item 3"))

But it works bad for "Item 2-2". How should I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2025-02-18 21:43:53

我制作了此简单的findID方法,以找到标题并返回idundefined作为数据阵列结构的结果。

findid(标题,列表)函数。
其目的是迭代第二个参数中收到的列表,在每个元素中查找标题,如果有匹配项,则返回ID,否则检查是否在元素中和元素中检查subs这种情况将其递归称为findID函数,使用元素的subs作为列表。如果找到一个元素,则返回其ID,否则迭代会继续。

假设每个标题在数组中都是唯一的。
否则只会找到第一个。

看以下

function findId(title, list) {
  for (const _item of list) {
    if (_item.title === title) {
      // Return some top-level id
      return _item.id;
      
    // If the item is not the one, check if it has subs
    } else if (_item?.subs) { 
    
      // Use _item.subs as a list, calling the function recursively
      const subId = findId(title, _item.subs);
      
      // Return some nested subId if found
      if (subId !== undefined) return subId;
    }
  }
  // Return undefined if not found
  return undefined;
}


var myData = [
{
    id: 0,
    title: "Item 1"
  }, 
  {
    id: 1,
    title: "Item 2",
    subs: [
    {
      id: 10,
      title: "Item 2-1",
      subs: [
      {
        id: 100,
        title: "Item 2-2-1"
      }, {
        id: 110,
        title: "Item 2-2-2"
      }, {
        id: 120,
        title: "Item 2-2-3"
      }]
    }, {
      id: 11,
      title: "Item 2-2"
    }, {
      id: 12,
      title: "Item 2-3"
    }]
  }, 
  {
    id: 2,
    title: "Item 3"
  },
  // more data here
];

console.log("Id: ", findId("Item 2", myData));
console.log("Id: ", findId("Item 2-1", myData));
console.log("Id: ", findId("Item 2-2-2", myData));
console.log("Id: ", findId("Wrong Title", myData));
console.log("Id: ", findId("Item 2", myData));<br/>
console.log("Id: ", findId("Item 2-1", myData));<br/>
console.log("Id: ", findId("Item 2-2-2", myData));<br/>
console.log("Id: ", findId("Wrong Title", myData));<br/>

最后一个返回未定义的,因为该标题不包括在数组中。

I made this simple findId method in order to find the title and returns the id or undefined as result made for your data array structure.

findId(title, list) function.
Its purpose is to iterate the list received in the second parameter, look for the title in each element and if there is a match it returns the id, otherwise it checks if there are subs in the element and in that case it is called recursively the findId function using the subs of the element as a list. If an element is found, its id is returned, otherwise the iteration continues.

This will work fine, assuming each title is unique in the array.
Otherwise only the first will be found.

Take a look at the following

function findId(title, list) {
  for (const _item of list) {
    if (_item.title === title) {
      // Return some top-level id
      return _item.id;
      
    // If the item is not the one, check if it has subs
    } else if (_item?.subs) { 
    
      // Use _item.subs as a list, calling the function recursively
      const subId = findId(title, _item.subs);
      
      // Return some nested subId if found
      if (subId !== undefined) return subId;
    }
  }
  // Return undefined if not found
  return undefined;
}


var myData = [
{
    id: 0,
    title: "Item 1"
  }, 
  {
    id: 1,
    title: "Item 2",
    subs: [
    {
      id: 10,
      title: "Item 2-1",
      subs: [
      {
        id: 100,
        title: "Item 2-2-1"
      }, {
        id: 110,
        title: "Item 2-2-2"
      }, {
        id: 120,
        title: "Item 2-2-3"
      }]
    }, {
      id: 11,
      title: "Item 2-2"
    }, {
      id: 12,
      title: "Item 2-3"
    }]
  }, 
  {
    id: 2,
    title: "Item 3"
  },
  // more data here
];

console.log("Id: ", findId("Item 2", myData));
console.log("Id: ", findId("Item 2-1", myData));
console.log("Id: ", findId("Item 2-2-2", myData));
console.log("Id: ", findId("Wrong Title", myData));
console.log("Id: ", findId("Item 2", myData));<br/>
console.log("Id: ", findId("Item 2-1", myData));<br/>
console.log("Id: ", findId("Item 2-2-2", myData));<br/>
console.log("Id: ", findId("Wrong Title", myData));<br/>

The last one returns undefined as intended, since the title is not included in the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文