I have an array of objects like this below. As you can see, i have rows nested inside input and rows is also array of objects.
let input = [
{
"title": "ENGINEERING",
"rows": [
{ "risk": "P1", "radarLink": "rdar://92642113" },
{ "risk": "P2", "radarLink": "rdar://92694095" },
{ "risk": "P3", "radarLink": "rdar://92694235" },
],
},
{
"title": "ENVIRONMENT",
"rows": [
{ "risk": "P1", "radarLink": "rdar://92684289" },
{ "risk": "P2", "radarLink": "rdar://92695531" },
{ "risk": "P5", "radarLink": "rdar://92424550" },
],
},
]
First of all, i want to filter rows and only get those objects whose risk === 'P1'. Then i also want to create a new object named p2PlusRow which will hold the count of objects whose value of risk !== 'P1. This new object p2PlusRow will gave countas mentioned above. It also has a property named radarLink. This is nothing but concatenation of radarLink whose risk !== 'P1'
For e.g. in ENGINEERING, there are 2 objects whose risk is not equal to P1. the radar links of those are rdar://92694095 and rdar://92694235. So the concatenated value would be rdar://92694095&92694235 (each radar ticket is appended as shown below
let output = [
{
"title": "ENGINEERING",
"rows": [
{ "risk": "P1", "radarLink": "rdar://92642113" },
],
"p2PlusRow": {
"count": "2",
"radarLink": "rdar://92694095&92694235"
}
},
{
"title": "ENVIRONMENT",
"rows": [
{ "risk": "P1", "radarLink": "rdar://92684289" },
],
"p2PlusRow": {
"count": "2",
"radarLink": "rdar://92695531&92424550"
}
},
]
In order to achieve this result, i tried a few things but i am stuck on getting the count and radarLink values dynamically. Below is the tried code
let output = input?.map((obj) => ({
...obj,
rows: obj.rows.filter((row) => row.risk === 'P1',
p2PlusRow: { count: 2, radarLink: 'link' }
}));
As you can see p2PlusRow is static values as i have not figured out how to loop through rows and get count and radarLink values. can someone let me know how to proceed in this case.