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

177
Visualizações
How can I skip the label if the value label already exists on my state but the new value will be included?

I have a dropdown select field where I can choose different attributes value. The problem is when I try to store the selected data in the state I got twice the label which I don't need. I want if the label already exists in my state the labels will not include but the selected value will include.

My selected objects:

enter image description here

Expected Output:

{label: "colors"
    selected: Array(2)
         0: {label: 'Black', value: 1}
         1: {label: 'Green', value: 2}
      }, 
{label: "size"
       selected: Array(1)
         0: {label: 'M', value: 1}
} ]

I am trying like this but am confused about how can I skip the label if the value label already exists.


    const previousValue = (label: string, e: any) => {
        let isExist: any;
        if (newAttributes?.length > 0) {
            isExist = newAttributes.find((attr: any) => attr.label === label)
        }
        console.log(isExist);

        const newValue = { label: label, selected: e }
        setNewAttributes((oldArray: any): any => {
            if (isExist) {
                // confused how can I skip the label

            } else {
                return [...oldArray, newValue]
            }
        });
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use the method findIndex instead of find to find if the item exists in newAttributes array.

findIndex returns -1 when item doesnt exist in the array and returns the index of the item otherwise. You could use that index to get the Object and update it.

    const previousValue = (label: string, e: any) => {
      let itemIndex: any;
      if (newAttributes ? .length > 0) {
        itemIndex = newAttributes.findIndex((attr: any) => attr.label === label)
      }

      const newValue = {
        label: label,
        selected: e
      }
      setNewAttributes((oldArray: any): any => {
        if (itemIndex >= 0) {
          // item exists at newAttributes[itemIndex]

          let updatedArr = [...oldArray];
          updatedArr[itemIndex] = {
            ...updatedArr[itemIndex],
            selected: [...updatedArr[itemIndex].selected, e]
          }
          return updatedArr;
        } else {
          return [...oldArray, newValue]
        }
      });
    }

about 4 years ago · Juan Pablo Isaza 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