Reputation: 135
I have an input:
var modbusData = [
{ Type: "rtu", IdDevice: "1", Time: "11:01:00", Data: "1,12,23" },
{ Type: "tcp", IdDevice: "2", Time: "11:01:11", Data: "30,40,50" },
{ Type: "rtu", IdDevice: "1", Time: "11:02:00", Data: "5,10,21" },
{ Type: "tcp", IdDevice: "2", Time: "11:02:11", Data: "32,44,53" },
];
After I call:
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, {})
}
let groupedData = groupBy(modbusData, 'Type')
console.log(groupedData)
The result will be:
{
rtu: [
{ Type: 'rtu', IdDevice: '1', Time: '11:01:00', Data: '1,12,23' },
{ Type: 'rtu', IdDevice: '1', Time: '11:02:00', Data: '5,10,21' }
],
tcp: [
{ Type: 'tcp', IdDevice: '2', Time: '11:01:11', Data: '30,40,50' },
{ Type: 'tcp', IdDevice: '2', Time: '11:02:11', Data: '32,44,53' }
]
}
And now I want when there is no "Type" : "rtu" or "tcp" in the modbusData, the result will return an empty array like this:
{
rtu: [
{ Type: 'rtu', IdDevice: '1', Time: '11:01:00', Data: '1,12,23' },
{ Type: 'rtu', IdDevice: '1', Time: '11:02:00', Data: '5,10,21' }
],
tcp: []
}
Upvotes: 0
Views: 60
Reputation: 48600
You need to pass in your expected group keys as a primer object for the reducer.
>>> [ 'rtu', 'tcp' ]
<<< Object.fromEntries(expectedGroups.map(k => [k, []])) // { rtu: [], tcp: [] }
var modbusData = [
{ Type: "rtu", IdDevice: "1", Time: "11:01:00", Data: "1,12,23" },
{ Type: "rtu", IdDevice: "1", Time: "11:02:00", Data: "5,10,21" }
];
function groupBy(objectArray, property, expectedGroups = []) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, Object.fromEntries(expectedGroups.map(k => [k, []])))
};
let groupedData = groupBy(modbusData, 'Type', [ 'rtu', 'tcp' ]);
console.log(groupedData);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Here is a more streamlined version that utilizes ES5/6 constructs.
const modbusData = [
{ Type: "rtu", IdDevice: "1", Time: "11:01:00", Data: "1,12,23" },
{ Type: "rtu", IdDevice: "1", Time: "11:02:00", Data: "5,10,21" }
];
const groupBy = (arr, prop, requiredKeys = []) =>
arr.reduce((acc, obj) =>
({ ...acc,
[obj[prop]]: [...(acc[obj[prop]] ?? []), obj]
}), Object.fromEntries(requiredKeys.map(k => [k, []])));
const groupedData = groupBy(modbusData, 'Type', [ 'rtu', 'tcp' ]);
console.log(groupedData);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Upvotes: 2
Reputation: 20039
Try merging the alltypes
using spread operator
let alltypes = { rtu: [], tcp: [] }
let result = {...alltypes, ...groupedData}
var modbusData = [
{ Type: "rtu", IdDevice: "1", Time: "11:01:00", Data: "1,12,23" },
{ Type: "rtu", IdDevice: "1", Time: "11:02:00", Data: "5,10,21" },
];
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, {})
}
let groupedData = groupBy(modbusData, 'Type')
let alltypes = { rtu: [], tcp: [] }
let result = {...alltypes, ...groupedData}
console.log(result)
Upvotes: 2