I have an array of objects as shown below. As you can see, each object with name Engineering
, Environment
and others
has its radars
object. this radars
object has all
object in it and it should have p1
, p2
, p3+
and ‘totalin it. if any of this is missing, that means its count is 0. for e.g. with
Environment, the
p1, and
p2` are missing. that means it count is 0 and so on.
let input = [
{
"name": "Engineering",
"radars": {
"all": {
"p1": { "count": 4 },
"p2": { "count": 1 },
"p3+": { "count": 2 },
"total": { "count": 7 }
},
}
},
{
"name": "Environment",
"radars": {
"all": {
"p3plus": { "count": 1 },
"total": { "count": 1 },
}
}
},
{
"name": "others",
"radars": {
"all": {},
}
}
]
Here is what i need to get the output as. I need to create array of objects with keys being ‘P1,
P2,
P3+and
Total. Basically, i want to iterate the input object and get its
radarobject. Whatever is the count for
p1,
p2,
p3+ and 'total
, i need to pass them one by one to the result. for e.g. in first object with Engineering
, i have p1
, p2
, p3+
and ‘totalas
4,
1,
2,
7`. So these values are passed to each of the keys in the output. similarly next set of values are passed.
const output = [
{ "P1": [ 4, 0, 0 ] },
{ "P2": [ 1, 0, 0 ] },
{ "P3+": [ 2, 1, 0] },
{ "Total": [ 7, 1, 0 ] }
]
Can someone let me know how to achieve this. this is slightly complex than the normal looping of array of objects and i was not able to find anything specific to this complexity.