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

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.

43 thoughts on “Check a list of checkboxes if input value matches one of the values in different array”

  1. 367029 322029fantastic post, very informative. I wonder why the other experts of this sector do not notice this. You ought to continue your writing. Im confident, youve a terrific readers base already! 127637

    Reply
  2. positive and negative unit circle community theater synonym , Ivermectin medication [url=http://ivermectinsale.me/#]Ivermectin pills for sale[/url] positive affirmations meditation. community action partnership huntsville alabama community america englewood . community acquired pneumonia world health organization Barato Estrace en Ecuador, Comprar Estrace medicamento. Comprar Estrace sin receta Comprar Estrace genГ©rico [url=https://kit.co/rentpati/comprar-estrace-sin-receta-barato-estrace-en-ecuador#]DГіnde comprar Estrace 2 mg[/url] , positive feedback loop geology positive adjectives personality community acquired pneumonia zithromax [url=http://stetuskop.com/showthread.php?p=88653#post88653]bergen community college xray program nd[/url] e4635f4 , community action agency baltimore county , community west bank near me .

    Reply
  3. 785815 579928Oh my goodness! a great write-up dude. Thanks a good deal Nonetheless We are experiencing trouble with ur rss . Do not know why Not able to sign up to it. Maybe there is anybody getting identical rss dilemma? Anyone who knows kindly respond. Thnkx 734696

    Reply
  4. Thanks on your marvelous posting! I certainly enjoyed reading
    it, you’re a great author. I will be sure to bookmark your blog and
    will come back later on. I want to encourage you continue your great job, have a nice holiday weekend!

    Reply
  5. Thanks a lot for sharing this with all of us you actually understand what you’re talking
    about! Bookmarked. Please also talk over with my web site =).
    We may have a hyperlink trade contract among us

    Reply
  6. For most up-to-date information you have to go to see web
    and on world-wide-web I found this site as a most excellent web
    page for most recent updates.

    Reply
  7. Pretty section of content. I just stumbled upon your web site and in accession capital
    to assert that I acquire in fact enjoyed account
    your blog posts. Any way I will be subscribing to your
    augment and even I achievement you access consistently fast.

    Reply
  8. It’s perfect time to make some plans for the long run and it is time to be happy.

    I have learn this post and if I may just I want to counsel you some attention-grabbing
    issues or advice. Perhaps you could write subsequent articles
    relating to this article. I wish to learn more things approximately it!

    Reply
  9. I have been surfing online more than three hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my opinion, if all site owners and bloggers made good content as you
    did, the internet will be a lot more useful than ever before.

    Reply
  10. Good day! I know this is kinda off topic but I was wondering if you knew where I could get
    a captcha plugin for my comment form? I’m using the same blog platform as
    yours and I’m having trouble finding one? Thanks
    a lot!

    Reply
  11. I got this site from my friend who informed me concerning this web page and at
    the moment this time I am browsing this web site and reading very informative
    content at this time.

    Reply
  12. You can definitely see your skills in the work you write.
    The arena hopes for more passionate writers like you who are
    not afraid to mention how they believe. At all times go after your heart.

    Reply
  13. An impressive share! I have just forwarded this
    onto a colleague who had been conducting a little research on this.
    And he actually ordered me lunch because I found it for
    him… lol. So let me reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to discuss this issue here on your internet site.

    Reply

Leave a Comment