I have an array like this:
[
'pathOne/pathTwo/contentOne',
'pathOne/pathThree/pathFour/contentTwo',
'pathOne/pathThree/contentFive',
'pathFive/pathThree/contentThree',
'pathOne/pathTwo/contentFour'
]
I'm looking to have a string in alpha order and content first before the next sub-level:
pathFive
pathOne
I started to get this inside an array with lodash to do easiest operation on it but i couldn't get this.
Trying many things i found finally what i wanted.
var object = {};
var test = [
'pathOne/pathTwo/contentTwo',
'pathOne/pathTwo/contentOne',
'pathOne/contentThree',
'pathOne/pathThree/pathFour/contentFour'
];
Object.resolve = function(path, obj) {
return path.split('.').reduce(function(prev, curr) {
return prev ? prev[curr] : undefined
}, obj || self)
}
_.forEach(test, function(value) {
var arrayPath = _.split(value, '/');
var file = arrayPath.pop();
if(Object.resolve(arrayPath.join('.'), object) === undefined)
arrayPath.push(0);
else
arrayPath.push(arrayPath.length-1);
_.set(object, arrayPath, file);
});
console.log(object);
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.