Filtering and adding new attributes in array on the basis of certain conditions [duplicate]

I have a array as follows :

array = [
{
 "id":1,
 "type":"abc",
 "quantity":36,
 "code":"EDF",
 "period":"2023-04-01"
},
{
 "id":0,
 "type":"yrf",
 "quantity":40,
 "code":"SFDF",
 "period":"2023-04-02"
},
{
 "id":0,
 "type":"yrf",
 "quantity":22,
 "code":"SFDF",
  "period":"2023-04-02"
},
{
 "id":1,
 "type":"abc",
 "quantity":19,
 "code":"EDF"
 "period":"2023-04-01"
}
]

I want to check if for any two elements in array period, type, code is same, then I want to combine those elements. If for those elements if id is 1 then, then value of quantity should be set to a new attribute “quantity_{{code}}cy”. If id is 0 then quantity of that element should be set to “quantity{{code}}”. For example for above array my final array looks as follows:

finalArray=[
{
 "id":1,
  "type":"abc",
 "quantity_EDF_cy":36,
 "code":"EDF",
  "quantity_EDF":''
 "period":"2023-04-01"
},
{
 "id":0,
 "type":"yrf",
 "quantity_SFDF_cy":'',
  "quantity_SFDF":22
 "code":"SFDF",
 "period":"2023-04-02"
}
]

here for EDF, first and last element has same type “abc”, same “code” and same “period” and id is 1, so it is eligible of addition of new attributes as “quantity_EDF_cy”:36 and “quantity_EDF”:” — this should be empty for id 1 and if id is 0 then “quantity_EDF_cy”:” should be empty. How can I do that?