【发布时间】:2018-11-10 19:00:59
【问题描述】:
对于对象数组:
[
{id: 1, name: "test", tagId: 1},
{id: 2, name: "test", tagId: 15},
{id: 3, name: "test", tagId: 5},
]
需要将特定属性列表(tagId)缩减为唯一数组[1,15,5],调用一些批处理方法,例如对实体列表的API进行http请求:
async (ids) => await axios.get('http://apihost/tag', {id: ids})
对于对象的结果数组:
[
{id: 1, name: "tag1"},
{id: 15, name: "tag2"},
{id: 5, name: "tag3"},
]
最后需要通过ID属性将这个对象映射到由result.id => original.tagId匹配的原始对象数组,实际上是对两个数组进行SQL连接来得到这个(如https://github.com/mtraynham/lodash-joins):
[
{id: 1, name: "test", tagId: 1, tag: {id: 1, name: "tag1"}},
{id: 2, name: "test", tagId: 15, tag: {id: 15, name: "tag2"}},
{id: 3, name: "test", tagId: 5, tag: {id: 5, name: "tag3"}},
]
我已经为此编写了一个 PHP 库,其 API 如下:
new BulkMap(source).map(
'tagId',
'tag',
async (ids) => axios.get('http://apihost/tag', {id: ids})
);
但现在我在 JS 中需要这个。是否有任何 Javascript/NodeJS 库可以这样做?它看起来像是微服务中非常常用的模式。
【问题讨论】:
标签: javascript node.js microservices