转换对象列表并通过比较相同的 Id 值转换为不同的格式

Convert a List of Objects and convert into different format by comparing with same Id value

这是包含不同性别数据的列表。

[
  {
    "ageCode": 1,
    "ageDesc": "0-4",
    "q": 358,
    "s": 158,
    "gender": "M"
  },
  {
    "ageCode": 1,
    "ageDesc": "0-4",
    "q": 328,
    "s": 258,
    "gender": "F"
  },
  {
    "ageCode": 3,
    "ageDesc": "15-59",
    "q": 525,
    "s": 125,
    "gender": "M"
  },
  {
    "agCode": 4,
    "ageDesc": "60+",
    "q": 458,
    "s": 358,
    "gender": "F"
  }
]

以上列表需要根据ageCode相同合并为以下列表。它需要合并 ageCode 相同的 2 个对象。转换后的列表如下所示。

[
  {
    "ageCode": 1,
    "ageDesc": "0-4",
    "qM": 358,
    "sM": 158,
    "qF": 328,
    "sF": 258
  }
  {
    "ageCode": 3,
    "ageDesc": "15-59",
    "qM": 525,
    "sM": 125
  },
  {
    "agCode": 4,
    "ageDesc": "60+",
    "qF": 458,
    "sF": 358
  }
]

尝试过的解决方案:

  for(let item  of this.ageData) {
        if (this.ageData.find((i) => { i.agCode=== item.agCode})){

//}

    }

这里需要重复和多次for循环等问题,有什么有效的方法可以实现吗

请尝试下一步。

const data = [
  {
    "ageCode": 1,
    "ageDesc": "0-4",
    "q": 358,
    "s": 158,
    "gender": "M"
  },
  {
    "ageCode": 1,
    "ageDesc": "0-4",
    "q": 328,
    "s": 258,
    "gender": "F"
  },
  {
    "ageCode": 3,
    "ageDesc": "15-59",
    "q": 525,
    "s": 125,
    "gender": "M"
  },
  {
    "ageCode": 4,
    "ageDesc": "60+",
    "q": 458,
    "s": 358,
    "gender": "F"
  }
];

const combined = Object.values(data.reduce((result, item) => {
if (!result[item.ageCode]) {
  result[item.ageCode] = {ageCode: item.ageCode, ageDesc: item.ageDesc};
}
result[item.ageCode]['s' + item.gender] = item.s;
result[item.ageCode]['q' + item.gender] = item.q;
return result;
}, {}));

console.log(combined);

如果您需要总结数字,请使用下一个:

    result[item.ageCode]['s' + item.gender] = result[item.ageCode]['s' + item.gender] ? result[item.ageCode]['s' + item.gender] + item.s : item.s;
    result[item.ageCode]['q' + item.gender] = result[item.ageCode]['q' + item.gender] ? result[item.ageCode]['q' + item.gender] + item.q : item.q;

和回来的路

const combined = [
  {
    "ageCode": 1,
    "ageDesc": "0-4",
    "qM": 358,
    "sM": 158,
    "qF": 328,
    "sF": 258
  },
  {
    "ageCode": 3,
    "ageDesc": "15-59",
    "qM": 525,
    "sM": 125
  },
  {
    "ageCode": 4,
    "ageDesc": "60+",
    "qF": 458,
    "sF": 358
  }
];

const original = combined.reduce((result, item) => {
  if (item.qM !== undefined) {
    result.push({
      "ageCode": item.ageCode,
      "ageDesc": item.ageDesc,
      "q": item.qM,
      "s": item.sM,
      "gender": "M"
    });
  }
  if (item.qF !== undefined) {
    result.push({
      "ageCode": item.ageCode,
      "ageDesc": item.ageDesc,
      "q": item.qF,
      "s": item.sF,
      "gender": "F"
    });
  }
  return result;
}, []);

console.log(original);

这是一个解决方案:

  listContact = [
    {
      "ageCode": 1,
      "ageDesc": "0-4",
      "q": 358,
      "s": 158,
      "gender": "M"
    },
    {
      "ageCode": 1,
      "ageDesc": "0-4",
      "q": 328,
      "s": 258,
      "gender": "F"
    },
    {
      "ageCode": 3,
      "ageDesc": "15-59",
      "q": 525,
      "s": 125,
      "gender": "M"
    },
    {
      "ageCode": 4,
      "ageDesc": "60+",
      "q": 458,
      "s": 358,
      "gender": "F"
    }
  ];
  listContact2 = [
    {
      "ageCode": 1,
      "ageDesc": "0-4",
      "qM": 358,
      "sM": 158,
      "qF": 328,
      "sF": 258
    },
    {
      "ageCode": 3,
      "ageDesc": "15-59",
      "qM": 525,
      "sM": 125
    },
    {
      "ageCode": 4,
      "ageDesc": "60+",
      "qF": 458,
      "sF": 358
    }
  ];
  uniqueListContact = [];
  flags = {};

...
this.uniqueListContact = [...this.listContact, ...this.listContact2];
this.uniqueListContact = this.uniqueListContact.filter((entry) => {
    if (this.flags[entry.ageCode]) {
        return false;
    }
    this.flags[entry.ageCode] = true;
    return true;
});

这是一个完整的工作示例: https://stackblitz.com/edit/angular-merge-and-distinct-array-from-property?file=src/app/app.component.ts