I have a function that changes an object for me and I'm wondering how a bit of it works and would be grateful if someone could explain it or point me in the right direction. Here is the function:
$scope.filteredObject = Object.keys($scope.filterObject).reduce(function(p, collection) {
var values = $scope.filterObject[collection];
p[collection] = Object.keys(values).filter(function(key) {
return values[key] === true;
}).map(function(key) {
return key;
});
return p;
}, {});
So this works great, but I'm wondering what the }, {});
at the end of the function does exactly. Im not exactly sure the name of that and googleing " }, {}
after a function in javascript " seems to confuse the hell out of google (lol). Thanks!
}
- end of the anoymous function expression function(p, collection) { … }
,
- delimiter between multiple arguments {}
- (empty) object literal , the second argument )
- end of function invocation, closing parentheses for the arguments list to .reduce(…)
It is an empty object and has nothing to do with the function. Have a look at the Array.prototype.reduce()
The reduce function has a second optional parameter.
arr.reduce(callback[, initialValue])
So in your case it's like this:
callback = function(p, collection) { /*...*/ };
initialValue = {}; // could also be new Object()
Hope this helps :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.