I have an 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"
}
]
For any two elements in the array, I want to combine the elements if period
, type
and code
are the same. For those elements, if id
is 1
then, then the value of quantity
should be set to a new attribute, quantity_{{code}}_cy
. If id
is 0
, then the quantity
of that element should be set to quantity_{{code}}
. For example, for the above array, my final array should look like:
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
, the first and last elements have the same type
(abc
), the same code
and the same period
and the id
is 1
, so it is eligible for the addition of new attributes "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 achieve this?