Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// map an object to an object
// Usage
// keep keys the same:
// @stevekrouse.objectMap({ a: 1 }, (key, value) => value + 1)
// change keys:
// @stevekrouse.objectMap({ a: 1 }, (key, value) => [key + "2", value + 1])
export const objectMap = async (obj, fn) =>
Object.fromEntries(
await Promise.all(
Object.entries(obj).map(async ([k, v]) => {
let res = await fn(k, v);
if (Array.isArray(res) && res.length === 2) {
return res;
}
else {
return [k, res];
}
}),
),
);
October 23, 2023