Pro AngularJS - Could you help explain part of this code?

后端 未结 2 948
醉酒成梦
醉酒成梦 2021-01-15 12:03

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

2条回答
  •  星月不相逢
    2021-01-15 12:27

    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.

提交回复
热议问题