0

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: []
}
0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.