I am reading a book called Pro AngularJS by Apress and I am just trying to ensure I understand all of the code and I am a bit baffled by the following code.
Below is
Let us see the first line return function(data, propertyName)
here data is the array of objects to be filtered against propertyName(category in this case)
.
Then we define var keys = {}
i.e, an empty object.
Now through for loop we are putting the value of propertyName(category in this case)
into variable val
.
So for example the first object of data array is like this [{ product: "product 1", category: 'Category 3'}, ....]
Thus the value of val = data[i][propertyName]
translates to data[0][category]
, which evaluates to Category 3.
Now the line angular.isUndefined(keys['Category 3'])
would evaluate to true
for if
condition (here we are asking if the given condition is undefined, which evaluates to true, thus if condition passes).
Within if loop we set the keys[val] = true
, so that again this category name is not pushed to the results array.