I have json array like this.. :
{
mycollection : [
{ module : "SD", severity : "high", count : 1 },
{ module : "SD", severity : "veryhigh", count : 1 },
{ module : "SD", severity : "critical", count : 1 },
{ module : "SD", severity : "high", count : 1 },
{ module : "SD", severity : "veryhigh", count : 1 },
{ module : "FICO", severity : "high", count : 1 },
{ module : "FICO", severity : "veryhigh", count : 1 },
{ module : "FICO", severity : "high", count : 1},
{ module : "FICO", severity : "veryhigh", count : 1 },
{ module : "FICO", severity : "critical", count : 1 }
]
};
i need output like below iam using javascript(sapui5) is there any way to achieve this?? :
{
mycollection : [
{ module : "SD", severity : "high", count : 2 },
{ module : "SD", severity : "veryhigh", count : 2 },
{ module : "SD", severity : "critical", count : 1 },
{ module : "FICO", severity : "high", count : 2 },
{ module : "FICO", severity : "veryhigh", count : 2 },
{ module : "FICO", severity : "critical", count : 1 }
]
};
plz Help
Hi Rajeesh,
Please create another array myCollectionNew and a variable myCollectStrcut {module:"",severity"",count:""} (P.S. you have to make myCollectStruct.length = 0 after its defination to remove the first empty element.)
Add the first element of the myCollection to myCollectionNew using the myCollectSTruct variable. (You can add the variable to the array through push method).
Now traverse through the myCollection comparing element 1 and 2 through the end of the array(nested loop). When ever there is an match(a combination of module and severity), increase the count in myCollectionNew and if the element is new add the new element(using the myCollectStruct) to the myCollectionNew.
You will have the desired output in myCollectionNew.
Regards,
Jagan
Hi Rajeesh,
the Lo-Dash library is an excellent helper for these kind of problems.
Unfortunately I can't give you the complete solution of your problem right now. Maybe these functions will help you on your path to the solution ;-)
Add a comment