Js: Convert Dot String Array To Object Tree
I have a string array like this and trying to build a tree hierarch grouped with . notation. i tried using recursive function and 'set' function from lodash but could not get expec
Solution 1:
You could split the strings and use the parts as path to the nested array.
var array = ['catalog.product.name', 'catalog.product.description', 'catalog.product.status', 'catalog.product_attribute_set.name', 'catalog.product_attribute_set.type'],
result = [];
array.forEach(s => s
.split('.')
.reduce((object, value) => {
var item = (object.children = object.children || []).find(q => q.value === value);
if (!item) object.children.push(item = { value, label: value })
return item;
}, { children: result })
);
console.log(result);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Post a Comment for "Js: Convert Dot String Array To Object Tree"