Created
September 14, 2015 14:48
-
-
Save edtoken/753fa2c29f38795b5890 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Сделает вложенный объект | |
* in: [a,b,c,d] | |
* value out:{a:{b:{c:{d:value}}}} | |
* !value out:{a:{b:{c:{d:{}}}}} | |
* @param arr | |
* @returns {{}} | |
*/ | |
makeEmbeddedObject:function(arr, value){ | |
var out = {}; | |
var values = _.clone(arr).reverse(); | |
var addValueToObj = (typeof value !== 'undefined'); | |
while(values.length){ | |
var val = values.shift(); | |
var obj = {}; | |
if(addValueToObj && values.length == arr.length - 1){ | |
obj[val] = value; | |
}else{ | |
obj[val] = out; | |
} | |
out = obj; | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment