I need to extract from carriers the code of the items present in active array:
const carriers = [
{ id: 0, code: "gls" },
{ id: 1, code: "tnt" },
{ id: 2, code: "fedex" },
{ id: 3, code: "ups" },
{ id: 4, code: "postnl" },
];
const active = [0, 2, 3];
const active_code = [];
let result = carriers.filter((c) => active.includes(Number(c.id)));
console.log(result);
result.forEach((item) => {
active_code.push(item.code);
});
console.log(active_code);
Expected result:
["gls", "fedex", "ups"]
The above code works, but I'd like to learn if there's a better/easier/more elegant way to get the same result?
Thanks