Home > Net >  Perform array treatment with reduce
Perform array treatment with reduce

Time:07-02

I have an array that i need to treat to extract and form a new object, this is the code i run right now:

data_proces = Object.values(selectedData.reduce((r, { Code, Description, Price}) => {    
    r[Description] ??= { Code, Description, Units: 0, Price: 0 , Total: 0};
    r[Description].Code= Code;
    r[Description].Units  ;
    r[Description].Price = Price ;
    r[Description].Total  = Price ;
    return r;
}, {}));

This gives me:

[{
    "Code": 0,
    "Description": "No Factured Act",
    "Units": 2,
    "Price": 0,
    "Total": 0
},
{
    "Code": 1,
    "Description": "Autopsy",
    "Units": 1,
    "Price": 44,
    "Total": 44
},
{
    "Code": 2,
    "Description": "Simple Biopsy",
    "Units": 3,
    "Price": 29,
    "Total": 87
},
{
    "Code": 1,
    "Description": "Citology",
    "Units": 4,
    "Price": 15,
    "Total": 60
},
{
    "Code": " -",
    "Description": "Free Act",
    "Units": 2,
    "Price": 56789,
    "Total": 91356
}]

And this is the result i want:

[{
    "Code": 0,
    "Description": "No Factured Act",
    "Units": 2,
    "Price": 0,
    "Total": 0
},

    "Code": 0,
    "Description": "No Factured Act",
    "Units": 1,
    "Price": 0,
    "Total": 0
},
{
    "Code": 1,
    "Description": "Autopsy",
    "Units": 1,
    "Price": 44,
    "Total": 44
},
{
    "Code": 2,
    "Description": "Simple Biopsy",
    "Units": 3,
    "Price": 29,
    "Total": 87
},
{
    "Code": 1,
    "Description": "Citology",
    "Units": 4,
    "Price": 15,
    "Total": 60
},
{
    "Code": " -",
    "Description": "Free Act",
    "Units": 1,
    "Price": 34567,
    "Total": 34567
},
{
    "Code": " -",
    "Description": "Free Act",
    "Units": 1,
    "Price": 56789,
    "Total": 56789
}]

As you can see, i need "No Factured Act" and "Free Act" to NOT sum up their units and stay as an individual values, how can i achieve this with Reduce?.

CodePudding user response:

You can simply create an array of Description properties that you don't want summed and create unique keys if the iterated description is included in the array. Here using the third index parameter of the reduce() callback.

const selectedData = [{ "Code": 0, "Description": "No Factured Act", "Price": 0, }, { "Code": 0, "Description": "No Factured Act", "Price": 0, }, { "Code": 1, "Description": "Autopsy", "Price": 44, }, { "Code": 2, "Description": "Simple Biopsy", "Price": 29, }, { "Code": 2, "Description": "Simple Biopsy", "Price": 29, }, { "Code": 2, "Description": "Simple Biopsy", "Price": 29, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": 1, "Description": "Citology", "Price": 15, }, { "Code": " -", "Description": "Free Act", "Price": 34567, }, { "Code": " -", "Description": "Free Act", "Price": 56789, }];

const noSum = ['No Factured Act', 'Free Act'];

const data_process = Object.values(
  selectedData.reduce((r, { Code, Description, Price }, i) => {

    let key = Description;
    if (noSum.includes(key)) {
      key = `${key}_${i}`;
    }

    r[key] ??= { Code, Description, Units: 0, Price, Total: 0 };
    r[key].Units  ;
    r[key].Total  = Price;

    return r;
  }, {})
);

console.log(data_process);

  • Related