Faizan Ali
Faizan Ali

Reputation: 1013

How to create a comparison expression using loop in Javascript?

I have following comparison expression:

return (!item['a'] || filter['a']) 
&& (!item['b'] || filter['b']) 
&& (!item['c'] || filter['c']);

But let's say I have a very long list of strings to be compared with && for e.g.

var myList = ['a','b','c','d']
for(var i in myList) {
   //(!item[myList[i]] || filter[myList[i]]) How am I suppossed to && this for every iteration?

} 

How am I suppossed to && the expression for every iteration?

Upvotes: 0

Views: 24

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370989

You could use use .every:

return ['a', 'b', 'c', 'd'].every(prop => !item[prop] || filter[prop]);

Upvotes: 3

Related Questions