Ad
Logic
Arrays
Data Types
Objects

A nice idea of using a lookup table could be improved with Map

Code
Diff
  • function mapOrder(array, order, key) {
      if (!Array.isArray(array)) return [];
      
      const lookupTable = array.reduceRight((table, o) => table.set(o[key], o), new Map());
      
      return order.map(k => lookupTable.get(k));
    }
    • function mapOrder(array, order, key) {
    • if (!Array.isArray(array)) return [];
    • const lookupTable = array.reduceRight(
    • (table, o) => {
    • table[o[key]] = o;
    • return table;
    • },
    • {}
    • );
    • const lookupTable = array.reduceRight((table, o) => table.set(o[key], o), new Map());
    • return order.map(k => lookupTable[k]);
    • return order.map(k => lookupTable.get(k));
    • }