1

It is as the title, but I am facing a problem!

I want to create getParentId(array, id) function.

This function get parent id by child id.

const array = [{
  id: 1,
  title: 'hello',
  children: [{
    id: 3,
    title: 'hello',
    children: [{
      id: 4,
      title:'hello',
      children: [
        { id: 5, title: 'hello'},
        { id: 6, title: 'hello'}
      ]
    },
    {
      id: 7,
      title: 'hello'
    }]
  }]
},
{
  id: 2,
  title: 'hello',
  children: [
    { id: 8, title: 'hello'}
  ]
}]
  • This array may nest indefinitely

Expected Result:

getParentId(array, 3) -> 1

getParentId(array, 5) -> 4

getParentId(array, 6) -> 4

getParentId(array, 8) -> 2

getParentId(array, 2) -> null

I would be grateful if you would send me information.

1
  • 2
    Welcome. Please share your attempts. Commented Dec 22, 2018 at 15:13

2 Answers 2

2

You could take a recursive approach by iterating the actual array and their children and stop if the id is found.

function getParentId(array, id, parentId = null) {
    return array.some(o => {
        if (o.id === id) return true;
        const temp = getParentId(o.children || [], id, o.id);
        if (temp !== null) {
            parentId = temp;
            return true;
        }
    })
        ? parentId
        : null;
}

const array = [{ id: 1, title: 'hello', children: [{ id: 3, title: 'hello', children: [{ id: 4, title:'hello', children: [{ id: 5, title: 'hello' }, { id: 6, title: 'hello' }] }, { id: 7, title: 'hello' }] }] }, { id: 2, title: 'hello', children: [{ id: 8, title: 'hello' }] }];

console.log(getParentId(array, 3)); // 1
console.log(getParentId(array, 5)); // 4
console.log(getParentId(array, 6)); // 4
console.log(getParentId(array, 8)); // 2
console.log(getParentId(array, 2)); // null
console.log(getParentId(array, 7)); // 3
console.log(getParentId(array, 4)); // 3
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

6 Comments

Can you explain why you check if parentId ... !== null at line 4?
that is part of the exit condition for some, to prevent having parentId with a non valid value.
Should also work without !== null since null is falsy anyway. But it can be argued that it is clearer this way.
the above code is not working for 7 and 4
@K.S, you are right. it was missing another variable and the need to overwrite parentId only if the returned value is not null. please see edit.
|
1

Nina Scholz's answer is great, but here's a slightly less functional approach (not using Array.some), if you like it better:

const array = [{id: 1, title: 'hello', children: [{id: 3, title: 'hello', children: [{id: 4, title:'hello', children: [{ id: 5, title: 'hello'}, { id: 6, title: 'hello'}]}, {id: 7, title: 'hello'}]}]}, {id: 2, title: 'hello', children: [{ id: 8, title: 'hello'}]}];

function getParentId(array, id, parentId = null) {
  // For every entry in the array
  for (const entry of array) {
    // If the ID matches, return the current parent ID
    if (entry.id === id) {
      return parentId;
    }
    // Otherwise, call the same function on its children, passing itself as the parent.
    // If there was a match, return it.
    if (entry.children && (deeperParentId = getParentId(entry.children, id, entry.id))) {
      return deeperParentId;
    }
  }
  // No match was found
  return null;
}

console.log(getParentId(array, 3));
console.log(getParentId(array, 5));
console.log(getParentId(array, 6));
console.log(getParentId(array, 8));
console.log(getParentId(array, 2));

Note that I overcommented it, which is not such a good idea when writing actual code; this is just for the answer.

Also, as I mentioned in the comments, please share your attempts next time.

1 Comment

This is exactly my deleted answer but with a couple of mistakes fixed. Now I see what I did wrong...

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.