Filter one array based on values of another, is this the best way? categories = [{id: "761601bc-4daf-4db2-a0cf-fe7f443fcb94", name: "Shoes"},{id: "601ebcfe-fcbd-4075-a4b4-a42e356c5747", name: "Hats"},{id: "238933cf-77a5-4443-b290-7d7c836f80ff", name: "Eyewear"}];

voucher = {id:1,categories:["761601bc-4daf-4db2-a0cf-fe7f443fcb94","601ebcfe-fcbd-4075-a4b4-a42e356c5747"]};

filterCategories(voucher) {
  const result = voucher.categories
    ? categories
        .filter(cat =&gt; {
          return voucher.categories.includes(cat.id);
        })
        .map(v =&gt; v.name)
        .sort()
        .join()
    : "";

  return result;
}


Expecting: "Hats,Shoes"

The result needs to be filtered and sorted and returned as a string. Is there a faster, more efficient way of doing this. Maybe using Lodash?
Normally there are a lot of categories, so I'd go through the voucher.categories array:

const filterCategories = (voucher, categories) =&gt; (
  Array.isArray(voucher.categories) ?
    voucher.categories
      .map(categoryId =&gt; {
        const categoryFound = categories.find(c =&gt; c.id === categoryId);
        return categoryFound ? categoryFound.name : null;
      })
      .filter(categoryName =&gt; categoryName !== null)
      .sort()
      .join() :
    ""
);


This way you don't have to go through the entire list of 'master' categories (which in practice may be very long) doing a search on the voucher categories in each iteration.

Note: it would be much faster if you could have the categories as an object with the id as index:

categories = {
  ["761601bc-4daf-4db2-a0cf-fe7f443fcb94"]: {name: "Shoes"},
  ["601ebcfe-fcbd-4075-a4b4-a42e356c5747"]: {name: "Hats"},
  ["238933cf-77a5-4443-b290-7d7c836f80ff"]: {name: "Eyewear"}
};

const filterCategories = (voucher, categories) =&gt; (
      Array.isArray(voucher.categories) ?
        voucher.categories
          .map(categoryId =&gt; {
            const categoryFound = categories[categoryId];
            return categoryFound ? categoryFound.name : null;
          })
          .filter(categoryName =&gt; categoryName !== null)
          .sort()
          .join() :
        ""
    );