I would like to explain my problem of the day.
I am currently making a request on postman (which works correctly)
My back
getPackIntegrations: {
rest: "GET /:id/integrations",
resource: {
name: "/packs/${id}",
scope: "webmessenger-integrations:get",
},
cache: false,
handler: async function (ctx) {
const res = await ctx.call("packs.get", {
id: ctx.params.id,
fields: "integrations",
query: {
...ctx.params.query,
},
});
return res;
},
},
postman's response
integrations : [{integration : xxx, entity : aaa}, {integration : yyy, entity : aaa},
{integration : zzz, entity : aaa}, {integration : 111, entity : bbb}, {integration : 222, entity : bbb}]
and I would rather receive the following format
[{entity: aaa, integrations : [xxx, yyy, zzz]}, {entity:bbb, integrations: [111,222]}]
How can I fix this issue?
thx for help
This code is far from optimal, but should at least do the trick. If someone else wants to improve upon my answer, that would be great.
let tmp_arr = [];
let final_arr = [];
res.integrations.map(
item => {
if(!tmp_arr[item.entity]){
tmp_arr[item.entity] = [];
}
tmp_arr[item.entity].push(item.integration);
}
);
for(const [k,v] of Object.entries(tmp_arr)){
final_arr.push(
{entity: k, integrations: v}
);
}
return final_arr;