Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node.js – How to merge objects inside an array based on condition?

In Node.js, I have 3 sets of data like

[
    {
        "userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3",
        "dailyData":159392.235451,
        "dailyDataInUSC":255.284807
    }
] 

and

[
    {
        "userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3",
        "monthlyData":159392.235451,
        "monthlyDataInUSC":255.284807
    }, 
    {
        "userId":"23fs6fds3-34k4-17de-3123-d2ec81e8aaf3",
        "monthlyData":349392.455451,
        "monthlyDataInUSC":655.234807
    }
] 

and

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3",
        "threeMonthsData":159392.235451,
        "threeMonthsDataInUSC":255.284807
    }, 
    {
        "userId":"23fs6fds3-34k4-17de-3123-d2ec81e8aaf3",
        "threeMonthsData":349392.455451,
        "threeMonthsDataInUSC":655.234807
    }, 
    {
        "userId":"34sdf34-67j4-54nd-6763-d2ec81e8aaf3",
        "threeMonthsData":6789392.455451,
        "threeMonthsDataInUSC":905.655807
    }
] 

How can I combine this to one object based on userId(filter) inside an array.

Eg, output should be like

[
    {
        "userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3",
        "dailyData":159392.235451,
        "dailyDataInUSC":255.284807,
        "monthlyData":159392.235451,
        "monthlyDataInUSC":255.284807,
        "threeMonthsData":159392.235451,
        "threeMonthsDataInUSC":255.284807
    }
]

Please help me to achieve this.

>Solution :

A combination of spread, reduce and findIndex can be used to solve the problem.

  • Combine the original arrays into a single array using the spread operator.
  • Use reduce to group the elements by key (in this case userId)

Something like this :

const dailyData = [{"userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3","dailyData":159392.235451,"dailyDataInUSC":255.284807}];
const monthlyData = [{"userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3","monthlyData":159392.235451,"monthlyDataInUSC":255.284807}, {"userId":"23fs6fds3-34k4-17de-3123-d2ec81e8aaf3","monthlyData":349392.455451,"monthlyDataInUSC":655.234807}]
const triMonthlyData = [{"userId":"54c7f3ef-64d4-40de-8100-d2ec81e8aaf3","threeMonthsData":159392.235451,"threeMonthsDataInUSC":255.284807}, {"userId":"23fs6fds3-34k4-17de-3123-d2ec81e8aaf3","threeMonthsData":349392.455451,"threeMonthsDataInUSC":655.234807}, {"userId":"34sdf34-67j4-54nd-6763-d2ec81e8aaf3","threeMonthsData":6789392.455451,"threeMonthsDataInUSC":905.655807}]


const combinedData = [...dailyData, ...monthlyData, ...triMonthlyData].reduce((mergedResult, curElement) => {
  let matchingElementIdx = mergedResult.findIndex(ele => ele.userId === curElement.userId);

  if (matchingElementIdx !== -1) {
    mergedResult[matchingElementIdx] = {...mergedResult[matchingElementIdx], ...curElement};
  } else {
    mergedResult = [...mergedResult, curElement];
  }
  return mergedResult;
}, []);

console.log(combinedData);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading