简体   繁体   English

JavaScript 搜索对象数组并返回父对象的索引

[英]JavaScript search Object Array and return index of parent

I have been looking around for a JavaScript method to return the index of a value but I can't seem to find one that works.我一直在寻找一种 JavaScript 方法来返回一个值的索引,但我似乎找不到一个有效的方法。

I have the following code:我有以下代码:

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

Is there a method to use on the topics variable to search through the entire object array at once for the value of Subtopic 1.1 and then return the parent index, which in this case would be 0.是否有一种方法可以在topics变量上使用一次在整个对象数组中搜索Subtopic 1.1的值,然后返回父索引,在这种情况下为 0。

There isn't a single function, but you can nest an Array.prototype.find function inside an Array.prototype.findIndex without issue to achieve what you want ( findIndex to search through the parents, find to search through the children):没有一个单一的功能,但你可以嵌套一个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

No.不。

You would iterate through children, then have a nested loop iterating through each index value.您将遍历子项,然后使用嵌套循环遍历每个索引值。 If you find a match, the incrementing variable from the parent loop is the index you want.如果找到匹配项,则来自父循环的递增变量就是您想要的索引。

edit: code example编辑:代码示例

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)
        }
    
}

You can use array.findIndex()您可以使用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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM