1

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

4 Answers 4

1

As a starter a tweak can be done by mapping it instead of creating a blank array and pushing to it.

carriers.filter(i=>active.includes(+i.id)).map(i=>i.code)
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is not the best solution, but, IMHO, it is the easiest to understand for who will facing the same issue with limited skills as I have
0

Map the active array by .finding the associated carrier.

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 result = active.map(id => carriers.find(c => c.id === id).code);
console.log(result);

For less computational complexity, turn the carriers into a structure indexed by ID first.

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 carriersById = Object.fromEntries(
  carriers.map(c => [c.id, c])
);
const result = active.map(id => carriersById[id].code);
console.log(result);

1 Comment

both perfect solution
0

1) You can achieve the result using Map.

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 map = new Map(carriers.map(({ id, code }) => [id, code]));
const active_code = active.map((id) => map.get(id));

console.log(active_code);

2) You can also get the result using Set

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 set = new Set(active);
const active_code = carriers
  .filter((o) => set.has(o.id))
  .map(({ code }) => code);

console.log(active_code);

Comments

0

I would map the carriers data to active_code directly.

You could also create an active_ids object to avoid calling includes for every entry.

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_ids = Object.fromEntries(active.map(i => [i, true]));

let active_code = carriers
    //.filter(c => active.includes(c.id))
    .filter(c => active_ids[c.id])         // faster if many items?
    .map(c => c.code)

console.log(active_code);

// expected ["gls", "fedex", "ups"]
console.assert(active_code[0] === "gls")
console.assert(active_code[1] === "fedex")
console.assert(active_code[2] === "ups")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.