我有一系列
var clickedImages = Session.get('clickedImages');
clickedImages = clickedImages.map( function(a) {return a.imageId;});
console.log("clickedImages Ids are: " ,...clickedImages);
产生:
clickedImages ID是:TaTg6hf3Gok5NsWYp XzGxS3LDtJ4DrXSdc 8CdapJoeSxSScHKFs
var TotalOfallImageIds = buyList.find().fetch().map(function(u) { return u._id; });
console.log(TotalOfallIds);
产生:
["yzML7ZLvkWkjBBvq5", "XzGxS3LDtJ4DrXSdc", "TaTg6hf3Gok5NsWYp", "8CdapJoeSxSScHKFs"]
但是,当我运行下面的代码时,为什么会出现错误?
(TotalOfallImageIds.indexOf(clickedImages) > -1);
上面的代码产生:false
如何正确使用带有几个元素的indexOf()?
最佳答案
.indexOf用于比较项目,因此,您需要遍历一个数组,而另一个遍历indexOf
var allClicked = true;
for (var i = 0; i < clickedImages.length; i++) {
var clickedImg = clickedImages[i];
allClicked = TotalOfallImageIds.indexOf(clickedImg) > -1 && allClicked;
}