From 6c9245ba0c748bb8425b6f4d53c8a2ab16c39aa0 Mon Sep 17 00:00:00 2001 From: Luk Date: Sat, 27 Jan 2024 21:07:10 +0800 Subject: [PATCH] add `objectize_array` method to convert an array of objects to a dictionary of objects by specified key --- coretool.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/coretool.js b/coretool.js index 3275456..da18f55 100644 --- a/coretool.js +++ b/coretool.js @@ -10,6 +10,10 @@ module.exports = { sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)), + objectize_array (arr, key) { + return arr.reduce((obj, item) => ({ ...obj, [item[key]]: item }), {}) + }, + deepFreeze (obj) { if (typeof obj !== 'object' || obj === null) { return obj