Search code examples
javascriptalgorithmobjectsearchnested

Find items and chech if it has the same id as in parent scope and if it has add isDisable to item


Hi everyone I need to get advice on how to realize such a function for searching and adding property if the children's scope has the same ids as the parent scope and add isDisable key.

The data which I got and I need to transform it with new property isDisable

const data = [
  {
   id: "MT1",
   children: []
  },
  {
   id: "MT2",
   children: []
  },
  {
   id: "MT4",
   children: []
  },
  {
   id: "1234",
   children: [
    {
     id: "MT1",
     children: []
    },
    {
     id: "MT65",
     children: []
    },
   ]
  },
  {
   id: "537465",
   children: [{
     id: "MT1",
     children: []
    },
    {
     id: "MT2",
     children: [
      {
       id: "MT1",
       children: []
      },
      {
       id: "MT12",
       children: []
      }
     ]
    },
   ]
  }
]

This is some function for searching and adding a property to an item and result.

const someSearchFunction = (data) => {}

console.log(someSearchFunction(data))

const data = [
  {
   id: "MT1",
   children: [],
   isDisable: false
  },
  {
   id: "MT2",
   children: [],
   isDisable: false
  },
  {
   id: "MT4",
   children: [],
   isDisable: false
  },
  {
   id: "MT12",
   children: [],
   isDisable: false
  },
  {
   id: "1234",
   children: [
    {
     id: "MT1",
     children: [],
     isDisable: true
    },
    {
     id: "MT65",
     children: [],
     isDisable: false
    },
   ]
  },
  {
   id: "537465",
   children: [
    {
     id: "MT1",
     children: [],
     isDisable: true
    },
    {
     id: "42354322",
     children: [
      {
       id: "MT1",
       children: [],
       isDisable: true
      },
      {
       id: "MT12",
       children: []
       isDisable: false
      }
     ]
    },
   ]
  }
]

Thanks!


Solution

  • We can do this by capturing the list of ids for the current level to pass on to the recursive call for our children.

    Here is a version which does not mutate the input -- we're not barbarians! -- but returns a new tree with the isDisable property set appropiately.

    const disableDupIds = (xs, ids = [], currLevel = xs .map (x => x .id)) => {
      return xs .map (({id, children, ...rest}) => ({
        id, 
        ...rest,
        isDisable: ids .includes (id),
        children: disableDupIds (children, currLevel)
      }))
    }
    
    const data = [{id: "MT1", children: []}, {id: "MT2", children: []}, {id: "MT4", children: []}, {id: "1234", children: [{id: "MT1", children: []}, {id: "MT65", children: []}]}, {id: "537465", children: [{id: "MT1", children: []}, {id: "MT2", children: [{id: "MT1", children: []}, {id: "MT12", children: []}]}]}]
    
    console .log (disableDupIds (data))
    .as-console-wrapper {max-height: 100% !important; top: 0}

    We first capture the list of ids in our current level, then simply map over our elements, returning new versions with isDisable set true when our current id is in the list from the previous level, and recurring on our children, using our blacklist of current level ids.