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

173
Views
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 answers
Answer question

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