How can I check for a value in a conditional array?

Nicholas

I have this piece of code that I would like to use

const traits = [{title:"hello", effect:"world"},{title:"foo", effect:"bar"}];
const [ traitsArray, settraitsArray ] = useState([]);

to be rendered as Checkbox element

{traits.map((items)=>{
   return <FormControlLabel
           label={items.title}
           key={items.title}
           control={<Checkbox data-value={items.title} checked={false} onChange={ e => {
              if(traitsArray.includes(e.target.getAttribute('data-value'))){
                 settraitsArray(prevState => prevState.filter(items => items !== e.target.getAttribute('data-value')))
              }
              else{
                 settraitsArray(prevState => prevState.concat(items))
              }
} }/>}/>})}

On checked item, I would like to check if any of the similar value exist in traitsArray, if true, delete the element from the array, and if false, add the element to the array.

But every single time I checked the item, it just keep on concatenating the array. Did I miss something, or did I code it the wrong way?

technicallynick

In your onChange function you are checking to see if your array includes a string but then if it doesn't you're adding an object. Your function should look like this:

(e) => {
  if(traitsArray.some(({title}) => title === items.title)){
   settraitsArray(prevState => prevState.filter(item => item.title !== items.title))
  } else {
    settraitsArray(prevState => prevState.concat(items))
  }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I write a template to check if a given value is in an array

From Dev

How can I check for the existance of an enum value in an array of Enums?

From Dev

How can I check If a nested array contains a value?

From Dev

How can I check for the existance of an enum value in an array of Enums?

From Dev

How can i check string value in String array?

From Dev

How can I loop through a Multidimensional array and check for a value, then display that array, or use the first in the array

From Dev

PHP : How can I check Array in array?

From Dev

How can I check the value is of type in typescript?

From Dev

How can i check a value is pasted in Textbox?

From Dev

How can I check that no letters are entered in the array?

From Dev

How can i check double array for uniqueness?

From Dev

How can I check that two array are equal or not?

From Dev

How can I check in chai an array + type?

From Dev

conditional operator to check same value in an array then filter it

From Dev

How can I check to see if any values in an array show a value of true with an if statement?

From Dev

C# How can I check if a multidimensional array has a value and it's index?

From Dev

C# How can I check if a multidimensional array has a value and it's index?

From Dev

How can I apply a conditional effect to a button based on value in the ViewModel?

From Dev

How can I set a conditional function argument value here?

From Dev

Can I check values in two columns and all rows to add a conditional value?

From Dev

How can I check ng-if value with one php value

From Dev

How can I check if one value is A or B and another value is A or B?

From Dev

How can I check if one value is A or B and another value is A or B?

From Dev

How can I check if javascript array already contains a specific array

From Dev

How can I check an array with a list of array values?

From Dev

How to check for a value in an array

From Dev

How can i access a Value in an Array with jQuery

From Dev

How can I get the mean value of an array?

From Dev

How can I select a value from an array?

Related Related

  1. 1

    How can I write a template to check if a given value is in an array

  2. 2

    How can I check for the existance of an enum value in an array of Enums?

  3. 3

    How can I check If a nested array contains a value?

  4. 4

    How can I check for the existance of an enum value in an array of Enums?

  5. 5

    How can i check string value in String array?

  6. 6

    How can I loop through a Multidimensional array and check for a value, then display that array, or use the first in the array

  7. 7

    PHP : How can I check Array in array?

  8. 8

    How can I check the value is of type in typescript?

  9. 9

    How can i check a value is pasted in Textbox?

  10. 10

    How can I check that no letters are entered in the array?

  11. 11

    How can i check double array for uniqueness?

  12. 12

    How can I check that two array are equal or not?

  13. 13

    How can I check in chai an array + type?

  14. 14

    conditional operator to check same value in an array then filter it

  15. 15

    How can I check to see if any values in an array show a value of true with an if statement?

  16. 16

    C# How can I check if a multidimensional array has a value and it's index?

  17. 17

    C# How can I check if a multidimensional array has a value and it's index?

  18. 18

    How can I apply a conditional effect to a button based on value in the ViewModel?

  19. 19

    How can I set a conditional function argument value here?

  20. 20

    Can I check values in two columns and all rows to add a conditional value?

  21. 21

    How can I check ng-if value with one php value

  22. 22

    How can I check if one value is A or B and another value is A or B?

  23. 23

    How can I check if one value is A or B and another value is A or B?

  24. 24

    How can I check if javascript array already contains a specific array

  25. 25

    How can I check an array with a list of array values?

  26. 26

    How to check for a value in an array

  27. 27

    How can i access a Value in an Array with jQuery

  28. 28

    How can I get the mean value of an array?

  29. 29

    How can I select a value from an array?

HotTag

Archive