ajmajmajma
ajmajmajma

Reputation: 14216

comparing arrays to dom elements check for same IDs

I am trying to compare 2 arrays to check if the same id exists in the array as on the elements in a ul i am comparing them to.

I am reducing down some incoming data (in the success of an ajax call) to just an array of ids like so

   var storeLibIds = data.libraries( function(obj){
            return obj.id;
    });

Then, I want to compare that array to some li's on the dom, and if the id exists in both - check off a check box inside the element

So I am tryin to loop through the li's like so

$.each(".lessonList li"), function(){

    //if $(this).attr("id") exists in storeLibIds, then $(this).find(".otherLibCheck").prop("checked", true); then check off the checkbox inside

        });

So my basic idea is to loop through the li's and check their arrt("id") vs the array and if it exists check off the box. The problem I am having is I'm not sure if I want to loop through all of them or if there is a way with .filter to check if exists to check off the checkbox. I am having trouble getting this logic down, any help wold be appreciated. Thanks!

Upvotes: 0

Views: 157

Answers (1)

BatScream
BatScream

Reputation: 19700

One way is to use storeLibIds.indexOf(id) to check if the id is present in the array.

$.each($(".lessonList li"), function(idx,obj){
      if (storeLibIds.indexOf(obj.attr("id")) != -1) {
           ...
});

Upvotes: 1

Related Questions