Is there any efficient way to construct an object from an array of string in LWC?
const layerArr = ['Comp Type', 'Region', 'Industry'];
OUTPUT
const targetObj = [
{
"name": "Comp Type",
"options": "comptypeOptions",
"taxLabel": "comptypeLabel"
},
{
"name": "Region",
"options": "regionOptions",
"taxLabel": "regionLabel"
},
{
"name": "Industry",
"options": "industryOptions",
"taxLabel": "industryLabel"
}
]
I am using reduce function but it's not woking here and also I want to use toLowerCase() on the fly while assigning the key too.
layerObject = this.layerOptions.reduce(function (result, item, index) {
result['name'] = item;
result['options'] = item.replace(' ', '').toLowerCase() + 'Options';
result['taxLabel'] = item.replace(' ', '').toLowerCase() + 'Label';
return result
}, {})