Home > database >  if name of array object same then combine array object in ts
if name of array object same then combine array object in ts

Time:09-06

I have an array of objects coming from backend.

var values = [
    {
      "name": "Patient Introductions",
      "series": [
        {
          "name": "Organization ABC",
          "value": 3
        }
      ]
    },
    {
      "name": "Patient Assessment",
      "series": [
        {
          "name": "Organization ABC",
          "value": 2.5
        }
      ]
    },
    {
      "name": "Patient Introductions",
      "series": [
        {
          "name": "Organization XYZ",
          "value": 2.5
        }
      ]
    },
    {
      "name": "Patient Assessment",
      "series": [
        {
          "name": "Organization XYZ",
          "value": 3.3
        }
      ]
    },
    
];

I want to combine the inner array's objects and get one single array of objects for same name of objects.

var output = [
    {
      "name": "Patient Introductions",
      "series": [
        {
          "name": "Organization ABC",
          "value": 3
        },
        {
          "name": "Organization XYZ",
          "value": 2.5
        }
      ]
    },
    {
      "name": "Patient Assessment",
      "series": [
        {
          "name": "Organization ABC",
          "value": 2.5
        },
         {
          "name": "Organization XYZ",
          "value": 3.3
        }
      ]
    },
];

I think, I need to use reduce but not sure how I can combine objects of series of same name.

Please help and guide. Thanks

CodePudding user response:

You are right with reducer

var values = [
    {
      "name": "Patient Introductions",
      "series": [
        {
          "name": "Organization ABC",
          "value": 3
        }
      ]
    },
    {
      "name": "Patient Assessment",
      "series": [
        {
          "name": "Organization ABC",
          "value": 2.5
        }
      ]
    },
    {
      "name": "Patient Introductions",
      "series": [
        {
          "name": "Organization XYZ",
          "value": 2.5
        }
      ]
    },
    {
      "name": "Patient Assessment",
      "series": [
        {
          "name": "Organization XYZ",
          "value": 3.3
        }
      ]
    },
];

var result = values.reduce((acc, curr) => {
  var existing = acc.find(element => element.name === curr.name);
  
  if(existing) {
    existing.series.push(...curr.series);
  } else {
    acc.push(curr);
  }
  
  return acc;
}, []);

console.log(result);

CodePudding user response:

  1. Make a new empty array

  2. loop through your original array if the name does not exist in the new array then add to it'

  3. if the name existed just add the new array to it

this way ensures no duplicate name will be in the array

var values = [
  {
    name: "Patient Introductions",
    series: [
      {
        name: "Organization ABC",
        value: 3,
      },
    ],
  },
  {
    name: "Patient Assessment",
    series: [
      {
        name: "Organization ABC",
        value: 2.5,
      },
    ],
  },
  {
    name: "Patient Introductions",
    series: [
      {
        name: "Organization XYZ",
        value: 2.5,
      },
    ],
  },
  {
    name: "Patient Assessment",
    series: [
      {
        name: "Organization XYZ",
        value: 3.3,
      },
    ],
  },
];


let newValues = [];
values.forEach((value) => {
  let index = newValues.findIndex((item) => item.name === value.name);
  if (index === -1) {
    newValues.push({ name: value.name, series: [value.series[0]] });
  } else {
    newValues[index].series.push(value.series[0]);
  }
});

console.log(newValues)

  • Related