[Solved] JS: Convert dot string array to object tree [duplicate]


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; }

solved JS: Convert dot string array to object tree [duplicate]