Hi i have an array of objects where i need to include new property or value to object in array based on occurrence
for example: In array of objects there is a property called order number, if object contains the highest orderNumber among the all objects then, add new property called color:red(high risk for understanding), if object contains second highest number among all object leaving the first highest then property called color:green(medium risk) , if third highest number among all objects then property called color:yellow(low risk), also if orderNumber is same then new property value mentioned will be same based on occurrence, and remaining values will have property called color:gray.(no risk)
Here is array :
data = [{
id: 181,
name: null,
orderNumber: 1
},
{
id: 182,
name: null,
orderNumber: 1
},
{
id: 183,
name: null,
orderNumber: 2
},
{
id: 184,
name: null,
orderNumber: 3
},
{
id: 185,
name: null,
orderNumber: 3
},
{
id: 186,
name: null,
orderNumber: 4
},
{
id: 188,
name: null,
orderNumber: 4
},
{
id: 189,
name: null,
orderNumber: 4
},
{
id: 190,
name: null,
orderNumber: 5
},
{
id: 191,
name: null,
orderNumber: 6
},
]
above in the array need to compare with orderNumber value,
Below is the O/P:
data = [{
id: 181,
name: null,
orderNumber: 1,
color: gray
},
{
id: 182,
name: null,
orderNumber: 1,
color: gray
},
{
id: 183,
name: null,
orderNumber: 2,
color: gray
},
{
id: 184,
name: null,
orderNumber: 3,
color: gray
},
{
id: 185,
name: null,
orderNumber: 3,
color: gray
},
{
id: 186,
name: null,
orderNumber: 4,
color: yellow
},
{
id: 188,
name: null,
orderNumber: 4,
color: yellow
},
{
id: 189,
name: null,
orderNumber: 4,
color: yellow
},
{
id: 190,
name: null,
orderNumber: 5,
color: green
},
{
id: 191,
name: null,
orderNumber: 6,
color: red
},
]
In above output color red is for orderNumber: 6 because it is greatest among all, and orderNumber: 5 is color green because it is second highest among all , same for orderNumber: 4 and remaining are gray, just need to assgin three color for first three highest values and remaining color is gray.
It's supposed to be in decreasing order.