0

I have a parse background job that contains a simple query.each for one class. This class has 2 Arrays field filled with objects.IDs. Inside this query, for every single object, i need to check if the objects.ID of the first Array are contained in the second Array. Basically in a simple loop:

    var j = 0;
    for (var j = 0; j < firstArray.length; j++) {
        if(firstArray[j] "isContainedIn" secondArray){
        // my custom code
        }
    }

What i can't figure out is the function to use, if exist..Does javascript have a function like that or i need to make a nested loop to achieve my goal?

EDIT: i worked it out using indexOf but the solution proposed by Shqiptar didn't work so here is the one that actually works:

first Array name = usersEligibleToVote second Array name = usersThatVoted

for (var j = 0; j < usersEligibleToVote.length; j++) {
        if(usersThatVoted.indexOf(usersEligibleToVote[j]) === -1){
            console.log("user.id "+usersEligibleToVote[j]+" needs to vote");
        } else {
            console.log("user.id "+usersEligibleToVote[j]+" has voted");
        }
}
Diego
  • 366
  • 3
  • 18

2 Answers2

1
var j = 0;
    for (var j = 0; j < firstArray.length; j++) {
        if(firstArray[j].contains(secondArray))
        {
        // your custom code here
        }
    }

And then for checking if an object is the same :

var j = 0;
    for (var j = 0; j < firstArray.length; j++) {
        if(firstArray[j].indexOf(secondArray) != -1) 
        {
        // your custom code here
        }
    }
0

I would process it through a simple jQuery and javascript for loop like so:

var arr1;
var arr2;
var i;
for(i = 0; i < arr1.length; i++) {
    if ($.inArray(arr1[i], arr2) {
        break; //do things here or what not
        }
}
dhershman
  • 341
  • 2
  • 12
  • You will be down voted for suggesting jQuery "for such a simple thing" hehe. Assuming that one currently uses it - than it's fair. – Mike Grabowski Nov 11 '14 at 19:11
  • No down votes :( It's technically only two lines :P It's much quicker for me to use jQuery inArray I use it for several product compares when going through product lists and promotions to grab them. – dhershman Nov 11 '14 at 19:13
  • Yep, the only thing is that some users may want to bundle the whole jQuery just to use this helper which is stupid as it's not the purpose of that plugin. For javascript logic operations and stuff -> checkout lodash or underscore. – Mike Grabowski Nov 11 '14 at 19:16