I think what’s confusing is that one might have forgotten that there are two ways to get a property value from an object. To find the value associated with the property sunglasses in the object inventory, we could use the dot operator; inventory.sunglasses returns the value 1900. But the brackets method works just as well; operator[‘sunglasses’] also returns the value 1900.
Given the line:
order.every(item => (inventory[item] >= item);
As explained elsewhere above, the index item points to each member of the array order, one at a time, as the .every iterator cycles thru the array. Now, each member of order is itself an array of two members. Therefore, during the first iteration item points to ['sunglasses, 2] , and item has the value ‘sunglasses’, and item, the value of 2.
Thus the statement : inventory[item] >= item,
- evaluates inventory[‘sunglasses’] and returns 1990, then
- compares that value with item, which is 2, and
- returns a value of true
On the second iteration, item points to the next member of the arrayorder: item is now ‘bags’, and item is 1. The result of the logic operator >= is again true, and the .every iterator, having cycled over the entire array order, returns a value of true