Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda