使用reduce格式化对象的格式数组?

时间:2019-09-04 16:53:29

标签: javascript arrays node.js object reduce

我有这个:

[
  [
    {
      users: {
        'ID': {
          stage: 21,
          city: 'london',
          data: 2345
        },
        'ID2': {
          stage: 21,
          city: 'london',
          data: 5325
        }
      }
    },
    {
      users: {
        'ID': {
          stage: 21,
          city: 'ny',
          data: 5761
        },
        'ID2': {
          stage: 21,
          city: 'ny',
          data: 5235
        }
      }
    },
    {
      users: {
        'ID': {
          stage: 21,
          city: 'stockholm',
          data: 7433
        },
        'ID2': {
          stage: 21,
          city: 'stockholm',
          data: 52365
        }
      }
    }
  ],
  [
    {
      users: {
        'ID': {
          stage: 22,
          city: 'london',
          data: 743
        },
        'ID2': {
          stage: 22,
          city: 'london',
          data: 5325
        },
      }
    },
    {
      users: {
        'ID': {
          stage: 22,
          city: 'ny',
          data: 152
        },
        'ID2': {
          stage: 22,
          city: 'ny',
          data: 61632
        },
      }
    },
    {
      users: {
        'ID': {
          stage: 13,
          city: 'stockholm',
          data: 2161
        },
        'ID2': {
          stage: 22,
          city: 'stockholm',
          data: 62176
        },
      }
    }
  ]
]

我想要这样的东西:

  [
    {
      id: 'ID',
      stages: {
        21: {
          cities: {
            london: {
              data: 2345
            },
            ny: {
              data: 5761
            },
            stockholm: {
              7433
            }
          }
        },
        22: {
          cities: {
            london: {
              data: 5325
            },
            ny: {
              data: 5235
            },
            stockholm: {
              52365
            }
          }
        }
      }
    },
    {
      id: 'ID2',
      // same idea for this user
    }
  ]

data仅作为示例,其中有更多元素,而不仅仅是一个

到目前为止,我一直尝试使用reduce map和/或flat

array.flat().map(ar => Object.values(ar.players)).flat()

但是我不确定将其展平是否是最好的主意,而且我不知道该怎么做,我尝试使用reduce,但是我的尝试都没有成功。

1 个答案:

答案 0 :(得分:0)

您需要嵌套的方法并为相同的组构建新对象。

var data = [[{ users: { ID: { stage: 21, city: 'london', data: 2345 }, ID2: { stage: 21, city: 'london', data: 5325 } } }, { users: { ID: { stage: 21, city: 'ny', data: 5761 }, ID2: { stage: 21, city: 'ny', data: 5235 } } }, { users: { ID: { stage: 21, city: 'stockholm', data: 7433 }, ID2: { stage: 21, city: 'stockholm', data: 52365 } } }], [{ users: { ID: { stage: 22, city: 'london', data: 743 }, ID2: { stage: 22, city: 'london', data: 5325 } } }, { users: { ID: { stage: 22, city: 'ny', data: 152 }, ID2: { stage: 22, city: 'ny', data: 61632 } } }, { users: { ID: { stage: 13, city: 'stockholm', data: 2161 }, ID2: { stage: 22, city: 'stockholm', data: 62176 } } }]],
    result = Object.values(data
        .flat()
        .reduce((r, { users }) => {
            Object.entries(users).forEach(([id, { stage, city, data }]) => {
                r[id] = r[id] || { id, stages: {} };
                r[id].stages[stage] = r[id].stages[stage] || { cities: {} };
                r[id].stages[stage].cities[city] = { data };
            });
            return r;
        }, {})
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }