如何使我的javaScript。 -映射功能更有效?
我目前正在将AWS OpenSearch搜索的大量数据返回到一个名为eventlist
的数组中。数组中的每个事件都有以下结构:
{
_source: {
"key": value
"key": value
"key": value
}
}
因此,我想破坏这一点,以使_Source
成为事件数据,然后通过EventList映射。目前,我有两个.map实例,我知道这不是那么有效,所以我想知道如何将它们结合起来。
let eventList = [
{
_source: {
tags: "xxx,yyy,zzz"
...
}
}
];
eventList = eventList.map((event) => event._source);
eventList = eventList.map((event) => {
const { tags } = event;
return {
...event,
tags: tags?.split(",") ?? [],
};
});
I'm currently returning a large set of data from AWS OpenSearch into an array called eventList
. Each event within the array has the following structure:
{
_source: {
"key": value
"key": value
"key": value
}
}
I therefore want to destructure this so that _source
becomes the event data and then map over eventList. Currently, I've got two .map instances, and I'm aware this isn't that efficient, so I'd like to know how to combine these.
let eventList = [
{
_source: {
tags: "xxx,yyy,zzz"
...
}
}
];
eventList = eventList.map((event) => event._source);
eventList = eventList.map((event) => {
const { tags } = event;
return {
...event,
tags: tags?.split(",") ?? [],
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在问题中所说的那样,您可以将这两个
MAP
调用:我在参数列表中使用了破坏性,但是如果您没有 不想要:
有一百种不同的方式来写这句话,但是您明白了。
实际上,我们可以进一步破坏:
但是所有这些变化都应大致相同。最大的胜利是只有一个
地图
。As you say in the question, you can combine those two
map
calls:I've used destructuring in the parameter list, but you don't have to do that if you don't want:
There are a hundred different ways to write that, but you get the idea.
Actually, we could take the destructuring further:
But all of these variations should be roughly the same in terms of performance; the big win is having just one
map
.