0

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.

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.

3 Answers 3

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can use 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'))

Comments

0

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

4 Comments

The topics variable is dynamic and can have X amount of children depending on the data retrieved from a database, would your way still work for that kind of functionality?
I've actually got an error in my code, but yea, the logic would work fine for any number of topics if the code was correct.
I've fixed it now, but I think all three answers here are valid.
Thank you for your response !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.