['a','b','c'].reduce(function(result, item, index, array) {
result[index] = item; //a, b, c
return result;
}, {}) //watch out the empty {}, which is passed as "result"
Pass an empty object {} as a starting point; then "augment" that object incrementally.
At the end of the iterations, result will be {"0": "a", "1": "b", "2": "c"}
If your array is a set of key-value pair objects:
[{ a: 1},{ b: 2},{ c: 3}].reduce(function(result, item) {
var key = Object.keys(item)[0]; //first property: a, b, c
result[key] = item[key];
return result;
}, {});
will produce: {a: 1, b: 2, c: 3}
For the sake of completeness, reduceRight allows you to iterate over your array in reverse order:
[{ a: 1},{ b: 2},{ c: 3}].reduceRight(/* same implementation as above */)
will produce: {c:3, b:2, a:1}
Your accumulator can be of any type for you specific purpose. For example in order to swap the key and value of your object in an array, pass []:
[{ a: 1},{ b: 2},{ c: 3}].reduce(function(result, item, index) {
var key = Object.keys(item)[0]; //first property: a, b, c
var value = item[key];
var obj = {};
obj[value] = key;
result.push(obj);
return result;
}, []); //an empty array
will produce: [{1: "a"}, {2: "b"}, {3: "c"}]
Unlike map, reduce may not be used as a 1-1 mapping. You have full control over the items you want to include or exclude. Therefore reduce allows you to achieve what filter does, which makes reduce very versatile:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Your array already is more-or-less just an object, but arrays do have some "interesting" and special behavior with respect to integer-named properties. The above will give you a plain object.
edit oh also you might want to account for "holes" in the array:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
if (arr[i] !== undefined) rv[i] = arr[i];
return rv;
}
In modern JavaScript runtimes, you can use the .reduce() method:
You could use an accumulator aka
reduce
.Pass an empty object
{}
as a starting point; then "augment" that object incrementally. At the end of the iterations,result
will be{"0": "a", "1": "b", "2": "c"}
If your array is a set of key-value pair objects:
will produce:
{a: 1, b: 2, c: 3}
For the sake of completeness,
reduceRight
allows you to iterate over your array in reverse order:will produce:
{c:3, b:2, a:1}
Your accumulator can be of any type for you specific purpose. For example in order to swap the key and value of your object in an array, pass
[]
:will produce:
[{1: "a"}, {2: "b"}, {3: "c"}]
Unlike
map
,reduce
may not be used as a 1-1 mapping. You have full control over the items you want to include or exclude. Thereforereduce
allows you to achieve whatfilter
does, which makesreduce
very versatile:will produce:
[{2: "b"}, {3: "c"}]
Caution:
reduce
andObject.key
are part ofECMA 5th edition
; you should provide a polyfill for browsers that don't support them (notably IE8).See a default implementation by Mozilla.
we can use
Object.assign
andarray.reduce
function to convert an Array to Object.If you're using jquery:
Here is an O(1) ES2015 method just for completeness.
Here's a recursive function I just wrote. It's simple and works well.
Here's an example (jsFiddle):
Results:
With a function like this:
Your array already is more-or-less just an object, but arrays do have some "interesting" and special behavior with respect to integer-named properties. The above will give you a plain object.
edit oh also you might want to account for "holes" in the array:
In modern JavaScript runtimes, you can use the
.reduce()
method:That one also avoids "holes" in the array, because that's how
.reduce()
works.