带有数组数组的打字稿对象

发布于 2025-02-04 12:25:31 字数 868 浏览 0 评论 0 原文

我需要一些帮助解决此问题:)

我想将对象传输到数组。预期的结果应该是:

result = [
  {
    id: 'test-1',
    message: 'test#1.1'
  },
  {
    id: 'test-1',
    message: 'test#1.2'
  },
  {
    id: 'test-2',
    message: 'test#2.1'
  },
  {
    id: 'test-2',
    message: 'test#2.2'
  }
]

我的明显解决方案是对objects.keys()和map()。不幸的是,这无法根据需要起作用:

mockData = {
  'test-1': [
    {
      message: 'test#1.1'
    },
    {
      message: 'test#1.2'
    }
  ],
  'test-2': [
    {
      message: 'test#2.1'
    },
    {
      message: 'test#2.2'
    }
  ]
}

const result = Object.keys(this.mockData).map((id) => {
  return {
    id,
    ...this.mockData[id],
  }
})

console.log(result)

我是否必须在this.mockdata [id]上放另一个映射()?我在做什么错,这里的最佳实践是什么(也许是Reple()?)?

我希望你能帮我

I need some help solving this :)

I want to transfer the object to an array. The expected result should be:

result = [
  {
    id: 'test-1',
    message: 'test#1.1'
  },
  {
    id: 'test-1',
    message: 'test#1.2'
  },
  {
    id: 'test-2',
    message: 'test#2.1'
  },
  {
    id: 'test-2',
    message: 'test#2.2'
  }
]

My apparent solution is with objects.keys() and map(). Unfortunately, this does not work as desired:

mockData = {
  'test-1': [
    {
      message: 'test#1.1'
    },
    {
      message: 'test#1.2'
    }
  ],
  'test-2': [
    {
      message: 'test#2.1'
    },
    {
      message: 'test#2.2'
    }
  ]
}

const result = Object.keys(this.mockData).map((id) => {
  return {
    id,
    ...this.mockData[id],
  }
})

console.log(result)

do I have to put another map() over this.mockData[id]? what am I doing wrong and what is best practice here (maybe reduce()?)?

I hope you can help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑咖 2025-02-11 12:25:31

要取消组,您可以 在分组的数组元素上,带有嵌套 map() map()的分组对象。

const mockData = { 'test-1': [{ message: 'test#1.1' }, { message: 'test#1.2' }], 'test-2': [{ message: 'test#2.1' }, { message: 'test#2.2' }] };

const result = Object.entries(mockData).flatMap(([id, vs]) => vs.map(v => ({ id, ...v })));

console.log(result);

或使用 for。 .. 如果您想循环

const mockData = { 'test-1': [{ message: 'test#1.1' }, { message: 'test#1.2' }], 'test-2': [{ message: 'test#2.1' }, { message: 'test#2.2' }] };

const result = [];
for (const [id, messages] of Object.entries(mockData)) {
  for (const message of messages) {
    result.push({ id, ...message });
  }
}

console.log(result);

To ungroup you can flatMap the Object.entries of the grouped object with a nested map() call over the grouped array elements.

const mockData = { 'test-1': [{ message: 'test#1.1' }, { message: 'test#1.2' }], 'test-2': [{ message: 'test#2.1' }, { message: 'test#2.2' }] };

const result = Object.entries(mockData).flatMap(([id, vs]) => vs.map(v => ({ id, ...v })));

console.log(result);

Or using a for...of loop if you'd rather

const mockData = { 'test-1': [{ message: 'test#1.1' }, { message: 'test#1.2' }], 'test-2': [{ message: 'test#2.1' }, { message: 'test#2.2' }] };

const result = [];
for (const [id, messages] of Object.entries(mockData)) {
  for (const message of messages) {
    result.push({ id, ...message });
  }
}

console.log(result);

超可爱的懒熊 2025-02-11 12:25:31

这将返回欲望解决方案,

const arr = [];
Object.keys(mockData).forEach((key) => {
    mockData[key].forEach((data) => {
        arr.push({
            id: key, 
            message: data.message
        })
    })
})

This will return the desire solution,

const arr = [];
Object.keys(mockData).forEach((key) => {
    mockData[key].forEach((data) => {
        arr.push({
            id: key, 
            message: data.message
        })
    })
})
可遇━不可求 2025-02-11 12:25:31
Object.keys(mockData)
    .map((id) =>
      mockData[id].map((l) => {
        return { ...l, id };
      })
    )
    .flat()
Object.keys(mockData)
    .map((id) =>
      mockData[id].map((l) => {
        return { ...l, id };
      })
    )
    .flat()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文