What I have:
[{title: hi}, {title: ha}, {title: ho}]
What I want:
{title: hi}, {title: ha}, {title: ho}
This is because, when I try to add the array to a database, like:
"$push" : { "paises" : array}
, it will be:
Array of objects
But I want this:
Object of objects
The solution:
var array = [{title: 'hi'}, {title: 'ha'}, {title: 'ho'}];
var object = {};
var arrayToObject = function(array, object){
array.forEach(function(element, index){
object[index] = element;
})
console.log(object);
}
arrayToObject(array, object);
https://jsfiddle.net/2r903tdh/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With