需要以不同的方式格式化对象数组

时间:2019-01-20 09:12:09

标签: javascript arrays typescript object

我有一个矩阵响应数组:

matrixArray = [
                {Responded: 1, RowName: row1, ColName: col1},
                {Responded: 2, RowName: row1, ColName: col2},
                {Responded: 0, RowName: row1, ColName: col3},
                {Responded: 0, RowName: row2, ColName: col1},
                {Responded: 0, RowName: row2, ColName: col2},
                {Responded: 1, RowName: row2, ColName: col3},
                {Responded: 1, RowName: row3, ColName: col1},
                {Responded: 0, RowName: row3, ColName: col2},
                {Responded: 1, RowName: row3, ColName: col3},
                ...
                ...
              ];

它告诉您某行已被响应多少次。 我需要以下格式的对象数组:

matrixArray = [
                {
                  RowName: row1,
                  col1: 1,           //Here '1' is no. of times column responded
                  col2: 2,
                  col3: 0
                },
                {
                  RowName: row2,
                  col1: 0,
                  col2: 0,
                  col3: 1
                },
                {
                  RowName: row3,
                  col1: 1,
                  col2: 0,
                  col3: 1
                },
              ];

我正在为此使用TypeScript。 谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用Array.reduceObject.values获得所需的结果:

const matrixArray = [
  {Responded: 1, RowName: 'row1', ColName: 'col1'},
  {Responded: 2, RowName: 'row1', ColName: 'col2'},
  {Responded: 0, RowName: 'row1', ColName: 'col3'},
  {Responded: 0, RowName: 'row2', ColName: 'col1'},
  {Responded: 0, RowName: 'row2', ColName: 'col2'},
  {Responded: 1, RowName: 'row2', ColName: 'col3'},
  {Responded: 1, RowName: 'row3', ColName: 'col1'},
  {Responded: 0, RowName: 'row3', ColName: 'col2'},
  {Responded: 1, RowName: 'row3', ColName: 'col3'}
];

const result = Object.values(matrixArray.reduce((result, entry) => {
  if (!(entry.RowName in result)) {
    result[entry.RowName] = {RowName: entry.RowName};
  }
  if (!(entry.ColName in result[entry.RowName])) {
    result[entry.RowName][entry.ColName] = 0;
  }
  result[entry.RowName][entry.ColName] += entry.Responded;
  return result;
}, {}));

console.log(result);

请注意,我没有在此处输入变量以使其可运行,请放心,因为您使用的是Typescript。

说明:

  • Array.reduce遍历数组,并通过在每次迭代中增加相应的RowName => {RowName, col1, col2, col3}数量来构建Responded映射(对象),
  • Object.values然后将其转换回数组。

答案 1 :(得分:1)

您可以像这样使用reduceObject.values

const matrixArray = [{Responded:1,RowName:'row1',ColName:'col1'},{Responded:2,RowName:'row1',ColName:'col2'},{Responded:0,RowName:'row1',ColName:'col3'},{Responded:0,RowName:'row2',ColName:'col1'},{Responded:0,RowName:'row2',ColName:'col2'},{Responded:1,RowName:'row2',ColName:'col3'},{Responded:1,RowName:'row3',ColName:'col1'},{Responded:0,RowName:'row3',ColName:'col2'},{Responded:1,RowName:'row3',ColName:'col3'}]

const merged = matrixArray.reduce((acc, {Responded,RowName,ColName}) => {
       acc[RowName] = acc[RowName] || {RowName};
       acc[RowName][ColName] = (acc[RowName][ColName] + Responded) || Responded;
       return acc;
   }, {});

const output = Object.values(merged);
console.log(output)

以下是上述代码的简化版本:

const matrix = [{Responded:1,RowName:'row1',ColName:'col1'},{Responded:2,RowName:'row1',ColName:'col2'},{Responded:0,RowName:'row1',ColName:'col3'},{Responded:0,RowName:'row2',ColName:'col1'},{Responded:0,RowName:'row2',ColName:'col2'},{Responded:1,RowName:'row2',ColName:'col3'},{Responded:1,RowName:'row3',ColName:'col1'},{Responded:0,RowName:'row3',ColName:'col2'},{Responded:1,RowName:'row3',ColName:'col3'}],
  
output = Object.values(matrix.reduce((a, {Responded,RowName,ColName}) => (
    (a[RowName] = a[RowName] || {RowName})[ColName] = Responded, a), {}));

console.log(output);