skip to Main Content

i have array:

[
 {id: 1, name: 'Parent1'},
 {id: 2, name: 'Child1', parent: {id: 1, name: 'Parent1'}},
 {id: 3, name: 'Child2', parent: {id: 1, name: 'Parent1'}},
 {id: 4, name: 'GrandChild1', parent: {id: 3, name: 'Child2'}},
 {id: 5, name: 'GrandChild2', parent: {id: 3, name: 'Child2'}}
]

I need to write a function with an id argument that will return an array that does not include the element with id and all of its children and children of childrens. Help pls

I tries write recursing function but fallen

4

Answers


  1. function filterByIdAndChildren(id, arr) {
      const filteredArray = [];
    
      function filterItems(inputId, array) {
        for (const item of array) {
          if (item.id !== inputId) {
            if (item.parent && item.parent.id === inputId) {
              filterItems(item.id, array);
            } else {
              filteredArray.push(item);
              if (item.parent) {
                filterItems(inputId, [item.parent]);
              }
            }
          }
        }
      }
    
      filterItems(id, arr);
      return filteredArray;
    }
    

    it should be work!

    Login or Signup to reply.
  2. const validArr = item => typeof item.age === 'number' && item.age > 17;
    
    const thisWontWork = arr => {
      return arr.filter(item => {
        if (item.id) {
          // here is the recursive call
          return thisWontWork(item.id).length > 0 && validArr(item);
        } else return validArr(item);
      });
    };
        
    log(thisWontWork(name));
    
    Login or Signup to reply.
  3. You could store the unwanted parents for later filtering out.

    This approach works for unsorted items.

    const
        filter = (array, id) => {
            const
                parents = Object.fromEntries(array.map(({ id, parent = {} }) => [id, parent.id])),
                isParent = pid => id === parents[pid] || pid in parents && isParent(parents[pid]);
            return array.filter(o => o.id !== id && !isParent(o.id));
        },
        data = [{ id: 1, name: 'Parent1' }, { id: 2, name: 'Child1', parent: { id: 1, name: 'Parent1' } }, { id: 3, name: 'Child2', parent: { id: 1, name: 'Parent1' } }, { id: 4, name: 'GrandChild1', parent: { id: 3, name: 'Child2' } }, { id: 5, name: 'GrandChild2', parent: { id: 3, name: 'Child2' } }];
    
    console.log(filter(data, 1));
    console.log(filter(data, 2));
    console.log(filter(data, 3));
    console.log(filter(data, 4));
    console.log(filter(data, 5));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  4. That is how I would approach the problem

    let values = [
     {id: 1, name: 'Parent1'},
     {id: 2, name: 'Child1', parent: {id: 1, name: 'Parent1'}},
     {id: 3, name: 'Child2', parent: {id: 1, name: 'Parent1'}},
     {id: 4, name: 'GrandChild1', parent: {id: 3, name: 'Child2'}},
     {id: 5, name: 'GrandChild2', parent: {id: 3, name: 'Child2'}},
     {id: 6, name: 'GreatGrandChild', parent: {id: 5, name: 'GrandChild2'}},
     {id: 11, name: 'Parent2'},
     {id: 12, name: 'Child1ToParent2', parent: {id: 11, name: 'Parent2'}},
     {id: 13, name: 'Child2ToParent2', parent: {id: 11, name: 'Parent2'}},
     {id: 14, name: 'GrandChild1ToParent2', parent: {id: 13, name: 'Child2ToParent2'}},
     {id: 15, name: 'GrandChild2ToParent2', parent: {id: 13, name: 'Child2ToParent2'}},
     {id: 16, name: 'GreatGrandChildToParent2', parent: {id: 15, name: 'GrandChild2ToParent2'}},
    ]
    
    function isDescendant(idRoot, node) {
        // If no parent, it is one of the roots of the family tree so we return false
        if (!node.parent) {
            return false;
        }
    
        // Else, finds the parent in the list.
        const parent = values.find(value => value.id === node.parent.id);
    
        //Checks if it is the right parent.
        if (idRoot === parent.id) {
            return true;
        }
    
        // Is a great children -> continue recursively
        return isDescendant(idRoot, parent);
    }
    
    let filteredValues = values.filter(x => isDescendant(1, x))
    console.log(filteredValues);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search