【问题标题】:JavaScript search Object Array and return index of parentJavaScript 搜索对象数组并返回父级索引
【发布时间】:2025-12-25 00:21:43
【问题描述】:

我一直在寻找一种 JavaScript 方法来返回值的索引,但我似乎找不到一个有效的方法。

我有以下代码:

let topics = [
    {
        id: 1,
        name: 'Topic 1',
        children: [
            {
               id: 2,
               name: 'Subtopic 1.1'  <---- Searching for this value
            }
        ]
    }
];

有没有一种方法可以在topics 变量上使用,一次在整个对象数组中搜索Subtopic 1.1 的值,然后返回父索引,在这种情况下为0。

【问题讨论】:

    标签: javascript arrays object search javascript-objects


    【解决方案1】:

    没有单个函数,但您可以将 Array.prototype.find 函数嵌套在 Array.prototype.findIndex 中,以实现您想要的(findIndex 搜索父母,find 搜索孩子):

    let topics = [{
        id: 1,
        name: 'Topic 1',
        children: [{
          id: 2,
          name: 'Subtopic 1.1'  // <---- Searching for this value
        }]
      },
      {
        id: 2,
        name: 'Topic 6',
        children: [{
          id: 5,
          name: 'Subtopic 1.7'
        }]
      },
      {
        id: 3,
        name: 'Topic 9',
        children: [{
          id: 4,
          name: 'Subtopic 1.192'
        },
        {
          id: 28,
          name: 'Subtopic 999'
        }],
      },
    ];
    
    function findParentIndex(name) {
      return topics.findIndex(topic => topic.children.find(child => child.name === name));
    }
    
    console.log(findParentId("Subtopic 1.192")); // 3
    console.log(findParentId("Subtopic 1.1")); // 1
    console.log(findParentId("Not in the list")); // -1

    【讨论】:

      【解决方案2】:

      您可以使用array.findIndex()

      let topics = [{
        id: 1,
        name: 'Topic 1',
        children: [{
            id: 1,name: 'Subtopic 1.2'
          }, {
            id: 4,name: 'Subtopic 1.4'
          }, {
            id: 2, name: 'Subtopic 1.1'
          }]
      }];
      
      const findIndexOf = val => {
        return topics[0].children.findIndex(e => e.name.trim() === val.trim())
      }
      
      console.log(findIndexOf('Subtopic 1.1'))

      【讨论】:

        【解决方案3】:

        没有。

        您将遍历子元素,然后有一个嵌套循环遍历每个索引值。如果找到匹配项,则父循环中的递增变量就是您想要的索引。

        编辑:代码示例

        let topics = [
            {
                id: 1,
                name: 'Topic 1',
                children: [
                    {
                       id: 2,
                       name: 'Subtopic 1.1' 
                    },
                    {
                        id: 2,
                        name: 'Subtopic 3.1' 
                     },
                     {
                        id: 2,
                        name: 'Subtopic 1.1' 
                     },
                     {
                        id: 2,
                        name: 'Subtopic 2.1' 
                     },
                     {
                        id: 2,
                        name: 'Subtopic 1.1' 
                     }
             
                    ]
            }
        ];
        
        
        for (let i in topics[0]["children"]) {
                if (topics[0]["children"][i]["name"] == "Subtopic 1.1") {
                    console.log(i)
                }
            
        }
        

        【讨论】:

        • topics 变量是动态的,根据从数据库中检索到的数据可以有 X 个子节点,您的方法仍然适用于这种功能吗?
        • 我的代码实际上有一个错误,但是是的,如果代码是正确的,这个逻辑对于任何数量的主题都可以正常工作。
        • 我现在已经修好了,但我认为这里的三个答案都是有效的。
        • 感谢您的回复!