Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

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.

about 4 years ago · Santiago Gelvez Report

0

Your expected output is not a valid array, better check it.

You can try this function:

const someSearchFunction = (data) => {
data.forEach(item => {
    item.isDisable = false
    item.children.some(child => {
        if (data.some(parent => {
            return parent.id === child.id
        })) {
            child.isDisable = true
        } else {
            child.isDisable = false
        }
    }
    )
})

}

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!