0

I have an array of

var clickedImages = Session.get('clickedImages'); 
clickedImages = clickedImages.map( function(a) {return a.imageId;});

console.log("clickedImages Ids are: " ,...clickedImages);

Which yields:

clickedImages Ids are: TaTg6hf3Gok5NsWYp XzGxS3LDtJ4DrXSdc 8CdapJoeSxSScHKFs

var TotalOfallImageIds =  buyList.find().fetch().map(function(u) { return u._id; });
console.log(TotalOfallIds); 

Which yields:

["yzML7ZLvkWkjBBvq5", "XzGxS3LDtJ4DrXSdc", "TaTg6hf3Gok5NsWYp", "8CdapJoeSxSScHKFs"]

However when I run the code below, why do I get a false?

(TotalOfallImageIds.indexOf(clickedImages) > -1); 

The code above yields: false

How do I correctly use indexOf() with several elements?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SirBT
  • 1,580
  • 5
  • 22
  • 51
  • Possible duplicate of [Check if every element in one array is in a second array](http://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array) – Tyler Roper Apr 17 '17 at 15:10
  • Loop over the clicked images and call `indexOf` with each of them. – Barmar Apr 17 '17 at 15:10
  • What do you want `TotalOfallImageIds.indexOf(clickedImages)` to do? Specifically, what is it meant to check for? – T.J. Crowder Apr 17 '17 at 15:11
  • Do you want to return true when each of the clicked image is are in the list of total image ids, or when at least one appears? – Fisch Apr 17 '17 at 15:11

2 Answers2

1

Because there is no entry in TotalOfallImageIds that is === the array of clicked image IDs referenced by clickedImages. No array is ever === to a string (because the result of === is always false when the operands are not the same type). indexOf uses === for comparisons.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

.indexOf is for comparing an item, so instead you need to iterate over one array and indexOf with the other

var allClicked = true;

for (var i = 0; i < clickedImages.length; i++) {
    var clickedImg = clickedImages[i];
    allClicked = TotalOfallImageIds.indexOf(clickedImg) > -1 && allClicked;    
}
Fred Johnson
  • 2,539
  • 3
  • 26
  • 52