-3
var array = [{
  "ADJUSTMENT_TYPE": "AP0001",
  "REV_TYPE": "R"
}, {
  "ADJUSTMENT_TYPE": "AP0001",
  "REV_TYPE": "E"
}, {
  "ADJUSTMENT_TYPE": "AP0001",
  "REV_TYPE": "N"
}];

我希望它是这样的

[{
  "ADJUSTMENT_TYPE": "AP0001",
  "REV_TYPE": "R",
  "E",
  "N"
}]

我想要这个在 javascript 中。谁能帮我

提前致谢

4

1 回答 1

0

当前示例不检查重复项REV_TYPE

const array = [
    { ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'R' },
    { ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'E' },
    { ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'N' },
    { ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'N' },
    { ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'N' },
    { ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'N' },
];

const result = array.reduce((acc, val) => {
    const findType = acc.find(t => t.ADJUSTMENT_TYPE === val.ADJUSTMENT_TYPE);

    if (findType) {
        findType.REV_TYPE.push(val.REV_TYPE);
    } else {
        acc.push({ ADJUSTMENT_TYPE: val.ADJUSTMENT_TYPE, REV_TYPE: [val.REV_TYPE] });
    }

    return acc;
}, []);

console.log(result);

于 2020-06-03T11:21:05.670 回答