This is fairly trivial to do with Array.prototype.reduce:
var arr = [
{ key: 'foo', val: 'bar' },
{ key: 'hello', val: 'world' }
];
var result = arr.reduce(function(map, obj) {
map[obj.key] = obj.val;
return map;
}, {});
console.log(result);
// { foo:'bar', hello:'world' }
Note: Array.prototype.reduce() is IE9+, so if you need to support older browsers you will need to polyfill it.
Top answer 1 of 16
746
This is fairly trivial to do with Array.prototype.reduce:
var arr = [
{ key: 'foo', val: 'bar' },
{ key: 'hello', val: 'world' }
];
var result = arr.reduce(function(map, obj) {
map[obj.key] = obj.val;
return map;
}, {});
console.log(result);
// { foo:'bar', hello:'world' }
Note: Array.prototype.reduce() is IE9+, so if you need to support older browsers you will need to polyfill it.
2 of 16
529
Using ES6 Map (pretty well supported), you can try this:
var arr = [
{ key: 'foo', val: 'bar' },
{ key: 'hello', val: 'world' }
];
var result = new Map(arr.map(i => [i.key, i.val]));
// When using TypeScript, need to specify type:
// var result = arr.map((i): [string, string] => [i.key, i.val])
// Unfortunately maps don't stringify well. This is the contents in array form.
console.log("Result is: " + JSON.stringify([...result]));
// Map {"foo" => "bar", "hello" => "world"}
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › from
Array.from() - JavaScript | MDN
To convert an ordinary object that's not iterable or array-like to an array (by enumerating its property keys, values, or both), use Object.keys(), Object.values(), or Object.entries(). To convert an async iterable to an array, use Array.fromAsync(). Array.from() never creates a sparse array.
javascript - How to convert Map to array of object? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Save this answer. ... Show activity on this post. You could take Array.from and map the key/value pairs. More on stackoverflow.com
Functional Programming - Use the map Method to Extract Data from an Array
Hi all, thanks for taking the time to look at my question 🙂 I’m having a bit of trouble understanding this function in the example, so i understand that the map function will iterate over users to extract the results i want. But what i dont understand is (user => user.name), why dont we ... More on forum.freecodecamp.org
Array turning into a Map-like object and some other questions
The MDN docs on Map do a pretty good job of spelling this out, particularly the Objets vs. Maps section. Technically you're using an array in your example, but arrays are just objects with some additional functionality so its basically the same thing. More on reddit.com
Spread operator not working when doing array.map on an array of objects?
It's the syntax of your callback. Change it to a.map(item => ({ ...item, someBool: true})) More on reddit.com
10:13
How to Use map() in JavaScript | Transform Arrays Easily with map() ...
02:14
How To Transform Arrays - JavaScript Array Map (In 2 Mins) - YouTube
19:16
JavaScript Array.map() explained in varying ranges of complexity ...
03:41
JavaScript Array Map Method - Basic Example & Real Project Example ...
07:02
JavaScript map() method in 7 minutes! 🗺️ - YouTube
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map
Map - JavaScript | MDN
August 13, 2026 - A Map object is iterated by key-value pairs — a for...of loop returns a 2-member array of [key, value] for each iteration. Iteration happens in insertion order, which corresponds to the order in which each key-value pair was first inserted into the map by the set() method (that is, there wasn't a key with the same value already in the map when set() was called).
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › map
Array.prototype.map() - JavaScript | MDN
July 12, 2026 - The map() method of Array instances creates a new array populated with the results of calling a provided function on every element in the calling array. const array = [1, 4, 9, 16]; // Pass a function to map const mapped = array.map((x) => x * 2); console.log(mapped); // Expected output: Array ...
Top answer 1 of 7
208
You could take Array.from and map the key/value pairs.
let map = new Map().set('a', 1).set('b', 2),
array = Array.from(map, ([name, value]) => ({ name, value }));
console.log(array);
2 of 7
57
Use Spread syntax and Array.map():
let myMap = new Map().set('a', 1).set('b', 2);
const arr = [...myMap].map(([name, value]) => ({ name, value }));
console.log(arr);
Mimo
mimo.org › glossary › javascript › map
JavaScript Map Function: Examples for Data Transformation
The .map() method creates a new array by calling a provided function on every element in the original array. It takes each element, transforms it according to the function, and adds the result to the new array. The original array remains unchanged. ... Become a full-stack developer.
Programiz
programiz.com › javascript › library › array › map
JavaScript Array map()
thisArg (optional) - Value to use as this when executing callback. By default, it is undefined. Returns a new array with elements as the return values from the callback function for each element. ... let newPrices = prices.map(Math.sqrt); // [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character
CodingBat
codingbat.com › java
CodingBat Java
Welcome to Codingbat. See help for the latest. Java Example Solution Code · Java String Introduction (video) Java Substring v2 (video) Java String Equals and Loops · Java String indexOf and Parsing · Java If and Boolean Logic · If Boolean Logic Example Solution Code 1 (video) If Boolean Logic Example Solution Code 2 (video) Java For and While Loops · Java Arrays and Loops · Java Map Introduction ·