带有数组数组的打字稿对象
我需要一些帮助解决此问题:)
我想将对象传输到数组。预期的结果应该是:
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()?)?
我希望你能帮我
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要取消组,您可以 在分组的数组元素上,带有嵌套
map()
map()
的分组对象。或使用
for。 ..
如果您想循环To ungroup you can
flatMap
theObject.entries
of the grouped object with a nestedmap()
call over the grouped array elements.Or using a
for...of
loop if you'd rather这将返回欲望解决方案,
This will return the desire solution,