Mohammad Areeb Siddiqui
Mohammad Areeb Siddiqui

Reputation: 10179

Looping through arrays and checking their values

What I have done is that I have firstly I have parsed them with the jQuery function, $.parseJSON() and so now I am having 2 arrays with 3 objects inside them. What I want to do is that I want to loop over both the arrays at the same time so that I could check the Post_IDs of the current object in the loop of both the arrays that if they are equal or not and then if I find that the Post_ID is not present there already or there is a mismatch then I want to do something.

Here's what I have done:

var posts = <? php echo $posts; ?> ;
setInterval(function () {
    $.get("admin/post/get_new_posts.php", {
        userId: <? php echo User::GetUserID($_SESSION["username"]); ?>
    }, function (data) {
        data = $.parseJSON(data);
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < posts.length; j++) {
                if (posts[j].Post_ID != data[i].Post_ID) {
                    //do something...
                } else {

                }
            }
        }
    });
}, 10000);

The posts variable has its value and I am sure about it by logging them into the console. Now what happens here is that every ten seconds a get request is sent and checks if the Post_ID meets of the posts variable object does not equal the Post_ID of the data variable then do something in there. But that's not the case. The condition is always going true.

And yes, here are my objects. The first one is the posts variable and the second one is the data variable.

the posts and data variables view

Any idea of how can I loop through both the variables or arrays and check for a property at the same time while looping?

Upvotes: 0

Views: 784

Answers (2)

mysticalnetcore
mysticalnetcore

Reputation: 181

You are executing two loops and this creates a problem. because they have the following meaning (I'm gonna translate them into pure english): for each item of array 'data', check any item in array 'posts' that is not equal to the current item of 'data'. This is why it is always "doing something".

Since both arrays have the same length, you should do one loop like this :

for (var i = 0; i < data.length; i++) {
            if (posts[i].Post_ID != data[i].Post_ID) {
                //do something...
            } else {

            }
    }

Upvotes: 0

Paul Roub
Paul Roub

Reputation: 36438

You're comparing every entry in posts to every entry in data. Most of those won't match - posts[0] won't match data[1], data[2], etc.

It sounds like you want to look at the corresponding members of each list (posts[0] vs. data[0], etc.)

In that case, you need one loop, not two.

// find the longest array's length
var maxi = Math.max(posts.length, data.length, 0);

for ( var i = 0; i < maxi; ++i )
{
  if (i >= posts.length)
  {
    // an entry in data, but not posts
  }
  else if (i >= data.length)
  {
    // an entry in posts, but not data
  }
  else if (data[i].Post_ID != posts[i].Post_ID)
  {
    // mismatched IDs
  }
  else
  {
    // matched IDs
  }
}

Upvotes: 1

Related Questions