@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
22 lines (21 loc) • 537 B
JavaScript
/**
* Returns new object with keys sorder in the given order.
* All keys that are not listed in `keyOrder` go last.
* Does not mutate original object.
*/
export function _sortObject(obj, keyOrder) {
const r = {};
// First, go over ordered keys
for (const k of keyOrder) {
if (k in obj) {
r[k] = obj[k];
}
}
// Second, go over all other keys
for (const [k, v] of Object.entries(obj)) {
if (keyOrder.includes(k))
continue;
r[k] = v;
}
return r;
}