【问题标题】:Node.js - How to merge objects inside an array based on condition?Node.js - 如何根据条件合并数组内的对象?
【发布时间】:2022-01-26 21:47:20
【问题描述】:

在 Node.js 中,我有 3 组数据,例如

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

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

[
    {
        "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
    }
] 

如何根据数组中的userId(filter) 将其组合到一个对象中。

例如,输出应该是这样的

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

请帮助我实现这一目标。

【问题讨论】:

标签: javascript node.js arrays object hash


【解决方案1】:

spreadreducefindIndex 的组合可用于解决问题。

  • 使用spread 运算符将原始数组组合成一个数组。
  • 使用reduce按键对元素进行分组(在本例中为userId

类似这样的:

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);

【讨论】:

    【解决方案2】:
    const aa = () => {
      let aa = [
        {
          userId: "54c7f3ef-64d4-40de-8100-d2ec81e8aaf3",
          dailyData: 159392.235451,
          dailyDataInUSC: 255.284807
        }
      ];
    
      let bb = [
        {
          userId: "54c7f3ef-64d4-40de-8100-d2ec81e8aaf3",
          monthlyData: 159392.235451,
          monthlyDataInUSC: 255.284807
        },
        {
          userId: "23fs6fds3-34k4-17de-3123-d2ec81e8aaf3",
          monthlyData: 349392.455451,
          monthlyDataInUSC: 655.234807
        }
      ];
    
      let cc = [
        {
          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
        }
      ];
    
      let newArrObj = aa;
      bb.forEach(item => {
        let index = newArrObj.findIndex(item1 => item1.userId === item.userId);
        if (index === -1) {
          newArrObj = [...newArrObj, item];
        } else {
          newArrObj[index] = { ...newArrObj[index], ...item };
        }
      });
      cc.forEach(item => {
        let index = newArrObj.findIndex(item1 => item1.userId === item.userId);
        if (index === -1) {
          newArrObj = [...newArrObj, item];
        } else {
          newArrObj[index] = { ...newArrObj[index], ...item };
        }
      });
      console.log(newArrObj);
    };
    

    【讨论】:

      猜你喜欢
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多