const array = ['part1-item1', 'part1-item2', 'part2-item1', 'part3-item1']
{
part1: ['item1', 'item2'],
part2: ['item1'],
part3: ['item1']
}
arr.reduce((acc,rec) => {
const [devId, paramId] = rec.split('-')
if (!acc[devId]) {
return {...acc, [devId]: [paramId]}
} else {
}
}, {})
<code>
const array = ['part1-item1', 'part1-item2', 'part2-item1', 'part3-item1'];
const result = array.reduce((acc, rec) => {
const [part, item] = rec.split('-');
if (!acc[part]) {
acc[part] = [item];
} else {
acc[part].push(item);
}
return acc;
}, {});
const array = ['part1-item1', 'part1-item2', 'part2-item1', 'part3-item1'];
const result = array.reduce((acc, rec) => {
const [devId, paramId] = rec.split('-');
if (!acc[devId]) {
acc[devId] = [paramId];
} else {
acc[devId].push(paramId);
}
return acc;
}, {});
console.log(result);