Check a list of checkboxes if input value matches one of the values in different array

Farah10

I have an array of objects (array1) that I am looping through and rendering in a form as a series of inputs where type="checkbox". I am setting the input value to the object.id, and the input name to the object.name.

I then have a second array (array2), which is just an array of values. I need to compare array1 to array2, and if the value for that given input matches one of the values in array2, then set checked="true." If not, then do not check. It's basically auto-checking any inputs that already have a value that exists in array2.

let array1 = [
{id: 28, name: "Action"}, 
{id: 35, name: "Comedy"},
{id: 80, name: "Crime"},
{id: 99, name: "Documentary"}
{id: 18, name: "Drama"}
{id: 10751, name: "Family"}
]

let array2 = [1, 65, 28, 12, 18]

Here's what I have for my loop:

for(let i in array2) {
   for(let j in array1) {
      if(array2[i] == array1[j].id) {
          return (
          <div>
             <label>name here</label>
             <input type="checkbox" id={array1[j].id} value={array1[j].id} name={array1[j].name} checked='true'/>
          </div>
          )
    }
      if(array2[i] != array1[j].id) {
           return (
           <div>
               <label>name here</label>
               <input type="checkbox" id={array1[j].id} value={array1[j].id} name={array1[j].name}/>
           </div>
          )
   }

Obviously this isn't working and sorry if my current code seems stupid. I've been at this for a while and I'm at a complete loss on how to make this work.

Drew Reese

You can map array1 to the checkbox inputs and do a simple check that array2 includes the id property from elements in array1.

{array1.map(({ id, name }) => (
  <label key={id}>
    {name}
    <input
      type="checkbox"
      value={id}
      name={name}
      defaultChecked={array2.includes(id)}
    />
  </label>
))}

Edit check-a-list-of-checkboxes-if-input-value-matches-one-of-the-values-in-different

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check a list of checkboxes if input value matches one of the values in different array

From Dev

Check if a table value in one model matches a table value in a different model

From Dev

Check search input if value matches in array and show respons

From Dev

How to find if a value matches one of the values from an array in Javascript

From Dev

How to find if a value matches one of the values from an array in Javascript

From Dev

JQuery - check checkboxes based on select list value

From Dev

Check input value in array

From Dev

Check if at least one input in array has a value using jQuery/Javascript

From Dev

Android - ListView - if i filter list having checkboxes it gets me different values in array

From Dev

check if the all values of array are different

From Dev

check if the all values of array are different

From Dev

Check array values are all different

From Dev

ActiveRecord check if any value in array matches value in array with postgres

From Dev

angularjs directive to check if min. one item in a list of checkboxes is selected

From Dev

selecting all checkboxes in javascript - with array of different values for checkbox

From Dev

What is the most efficient way to check if a value of a list is contained in one of the other values of the list?

From Dev

check if input matches current shown item in array with php

From Dev

Check if THIS clicked TD's value matches the value of an array

From Dev

Checkboxes list values in a form

From Dev

Java: Array list of objects & getting values/properties of automatically created checkboxes

From Dev

php - append value to array from array if values matches

From Dev

Check if an Item's Property's value matches any from a list

From Dev

Check if string array elements have different values

From Dev

Referencing the values in one array list to values in another

From Dev

Check if each array value matches result from database

From Dev

Loop through an array of names and check if value of keypress matches a character in a name

From Dev

input values into an array, using different class files

From Dev

PHP: How to compare keys in one array with values in another, and return matches?

From Dev

how to check equality of one value with two values?

Related Related

  1. 1

    Check a list of checkboxes if input value matches one of the values in different array

  2. 2

    Check if a table value in one model matches a table value in a different model

  3. 3

    Check search input if value matches in array and show respons

  4. 4

    How to find if a value matches one of the values from an array in Javascript

  5. 5

    How to find if a value matches one of the values from an array in Javascript

  6. 6

    JQuery - check checkboxes based on select list value

  7. 7

    Check input value in array

  8. 8

    Check if at least one input in array has a value using jQuery/Javascript

  9. 9

    Android - ListView - if i filter list having checkboxes it gets me different values in array

  10. 10

    check if the all values of array are different

  11. 11

    check if the all values of array are different

  12. 12

    Check array values are all different

  13. 13

    ActiveRecord check if any value in array matches value in array with postgres

  14. 14

    angularjs directive to check if min. one item in a list of checkboxes is selected

  15. 15

    selecting all checkboxes in javascript - with array of different values for checkbox

  16. 16

    What is the most efficient way to check if a value of a list is contained in one of the other values of the list?

  17. 17

    check if input matches current shown item in array with php

  18. 18

    Check if THIS clicked TD's value matches the value of an array

  19. 19

    Checkboxes list values in a form

  20. 20

    Java: Array list of objects & getting values/properties of automatically created checkboxes

  21. 21

    php - append value to array from array if values matches

  22. 22

    Check if an Item's Property's value matches any from a list

  23. 23

    Check if string array elements have different values

  24. 24

    Referencing the values in one array list to values in another

  25. 25

    Check if each array value matches result from database

  26. 26

    Loop through an array of names and check if value of keypress matches a character in a name

  27. 27

    input values into an array, using different class files

  28. 28

    PHP: How to compare keys in one array with values in another, and return matches?

  29. 29

    how to check equality of one value with two values?

HotTag

Archive