I am trying to use reduce
to convert a nested array to an object.
I want to convert var bookprice = [["book1", "$5"], ["book2", "$2"], ["book3", "$7"]];
to
var bookpriceObj = {
"book1": "$5",
"book2": "$2",
"book3": "$7"
};
here is what I tried
var bookprice = [["book1", "$5"], ["book2", "$2"], ["book3", "$7"]];
bookpriceObj = {};
bookprice.reduce(function(a, cv, ci, arr){
for (var i = 0; i < arr.length; ++i)
bookpriceObj [i] = arr[i];
return bookpriceObj ;
})
but the below result is not the desired result
{
["book1", "$5"]
["book2", "$2"]
["book3", "$7"]
}