Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda