Browse files

Object.setByPath function

  • Loading branch information...
1 parent 368b58f commit d6bbcec11f0e55df9f58711b3f1a494d1b77d0ba Ruslan Sukhar committed May 23, 2012
Showing with 13 additions and 0 deletions.
  1. +13 −0 Source/Types/Object.Extras.js
View
13 Source/Types/Object.Extras.js
@@ -31,6 +31,19 @@ var hasOwnProperty = Object.prototype.hasOwnProperty;
Object.extend({
+ setByPath: function(source, path, value){
+ if (typeof path == 'string') path = path.split('.');
+ var iterator = source;
+ for (var i = 0, l = parts.length - 1; i++){
+ if (i < l){
+ if (typeof iterator[parts[i]] != 'object') iterator[parts[i]] = {};
+ iterator = iterator[parts[i]];
+ } else {
+ iterator[parts[i]] = value;
+ }
+ }
+ },
+
getFromPath: function(source, parts){
if (typeof parts == 'string') parts = parts.split('.');
for (var i = 0, l = parts.length; i < l; i++){

0 comments on commit d6bbcec

Please sign in to comment.