【问题标题】:format array of object using reduce?使用reduce格式化对象数组?
【发布时间】:2020-01-07 14:35:39
【问题描述】:

我有这个:

[
  [
    {
      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,但我的尝试都没有接近成功。

【问题讨论】:

    标签: javascript arrays node.js object reduce


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2022-01-10
      相关资源
      最近更新 更多