This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #526 from sergi/deep_pluck
Implement `deepPluck`, a pluck operator for nested properties
- Loading branch information
Showing
3 changed files
with
114 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,26 @@ | ||
/** | ||
* Retrieves the value of a specified property from all elements in the Observable sequence. | ||
* @param {String} prop The property to pluck. | ||
* Retrieves the value of a specified nested property from all elements in | ||
* the Observable sequence. | ||
* @param {String} nestedProps The nested property to pluck. | ||
* @returns {Observable} Returns a new Observable sequence of property values. | ||
*/ | ||
observableProto.pluck = function (prop) { | ||
return this.map(function (x) { return x[prop]; }); | ||
observableProto.pluck = function () { | ||
var args = [].slice.call(arguments); | ||
var len = args.length; | ||
return this.map(function (x) { | ||
if (len === 0) { | ||
return undefined; | ||
} | ||
|
||
var currentProp = x; | ||
for (var i = 0; i < len; i++) { | ||
var p = currentProp[args[i]]; | ||
if (typeof p !== 'undefined') { | ||
currentProp = p; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
return currentProp; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters