My current Data structure is show below which is grouped based on a property called 'submitId'
{
bc3827d9c21548f8b1ae072ceab4ce6d: [
{...}
],
5ee08c723e1d40b0af716ea3e18c6114: [
{...}
],
886b149dfe1241efac863d0532b7d847: [
{...}
]
}
Actual data based on a single submitId is shown below
// ACTUAL DATA
{
bc3827d9c21548f8b1ae072ceab4ce6d: [
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'ATL',
indicator: 0,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'AAA',
indicator: 0,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'AAA',
indicator: 1,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'ATL',
indicator: 1,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
]
}
for a given 'submitId', the unique object is made up of unique combination of 'code', indicator is set as 1 if all objects have 1 and 0 for same reason BUT set to 'both' if a mix of 0 and 1. The uniques object needs to look like below:
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'ATL, AAA', <--- unique codes
indicator: 'both', <--- if indicator is even mix of 0 and 1 then 'both'
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
please help!
CodePudding user response:
This is called a reduce
operation : you want to convert an array of entries into a single entry. The Array.prototype.reduce is precisely what you want to use.
CodePudding user response:
Although there is only one set of "real" data to work with I hope that the following shows how you could tackle the task:
const data={
bc3827d9c21548f8b1ae072ceab4ce6d: [
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'ATL',
indicator: 0,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'AAA',
indicator: 0,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'AAA',
indicator: 1,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
{
status: 'FINISHED',
start: '2022-05-26 05:01:00.0',
end: '2022-05-28 05:01:00.0',
submissionRank: 112,
submitId: 'bc3827d9c21548f8b1ae072ceab4ce6d',
code: 'ATL',
indicator: 1,
addBy: 30,
delayBy: 0,
pstat: 'DATA GATHERED',
fstat: 'FINISHED',
},
]
};
const res=Object.fromEntries(Object.entries(data).map(([id,o])=>
[id,o.reduce((a,c,i)=>{
if(a.indicator!=c.indicator) a.indicator="both";
if(i==1)a.code=[a.code];
else a.code.push(c.code);
return a;
})]
));
console.log(res)
CodePudding user response:
You can use reduce and filter to resolve this task:
let obj = {
uuid: [
{
status: "FINISHED",
start: "2022-05-26 05:01:00.0",
end: "2022-05-28 05:01:00.0",
submissionRank: 112,
submitId: "bc3827d9c21548f8b1ae072ceab4ce6d",
code: "ATL",
indicator: 1,
addBy: 30,
delayBy: 0,
pstat: "DATA GATHERED",
fstat: "FINISHED",
},
{
status: "FINISHED",
start: "2022-05-26 05:01:00.0",
end: "2022-05-28 05:01:00.0",
submissionRank: 112,
submitId: "bc3827d9c21548f8b1ae072ceab4ce6d",
code: "AAA",
indicator: 1,
addBy: 30,
delayBy: 0,
pstat: "DATA GATHERED",
fstat: "FINISHED",
},
{
status: "FINISHED",
start: "2022-05-26 05:01:00.0",
end: "2022-05-28 05:01:00.0",
submissionRank: 112,
submitId: "bc3827d9c21548f8b1ae072ceab4ce6d",
code: "AAA",
indicator: 1,
addBy: 30,
delayBy: 1,
pstat: "DATA GATHERED",
fstat: "FINISHED",
},
{
status: "FINISHED",
start: "2022-05-26 05:01:00.0",
end: "2022-05-28 05:01:00.0",
submissionRank: 112,
submitId: "bc3827d9c21548f8b1ae072ceab4ce6d",
code: "ATL",
indicator: 0,
addBy: 30,
delayBy: 1,
pstat: "DATA GATHERED",
fstat: "FINISHED",
},
],
};
let one = false;
let zero = false;
let codes = [];
let res = obj.uuid.reduce((t, v, i) => {
let = indicator = v.indicator;
let = code = v.code;
if (indicator === 0) zero = true;
else if (indicator === 1) one = true;
if (zero && one) indicator = `both`;
else indicator = indicator;
v.indicator = indicator;
codes.push(v.code);
unq = codes.filter((v, i, a) => a.indexOf(v) === i);
v.code = unq.toString();
return v;
});
console.log(res);