javascript - convert map to array (items as key->value objects)
JavaScript[Edit]
+
0
-
0
JavaScript - convert map to array (items as key->value objects)
1 2 3 4 5 6 7 8 9 10const map = { a: 1, b: 2, c: 3 }; const keys = Object.keys(map); // <---- conversion const array = keys.map(key => ({key, value: map[key]})); // <---- conversion console.log(array);
Reset
[Edit]
+
0
-
0
JavaScript - convert map to array (items as key->value objects)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19const DEFAULT_MAPPER = (key, value) => ({key, value}); const toArray = (map, mapper = DEFAULT_MAPPER) => { const keys = Object.keys(map); return keys.map(key => mapper(key, map[key])); }; // Usage example: const map = { a: 1, b: 2, c: 3 }; const array = toArray(map, (key, value) => ({key, value})); console.log(array);