So I’m curious - with amounts
structured like that, what makes you think it’s an array? @OJL is right with his question, it’s not quite an array, and its not quite an object. It wants to be, but it is a very broken hybrid that should not actually work.
If it’s an array, it wouldn’t have each member defined like 0:"7800"
, as that is the definition for object properties. If it’s an object, then it wouldn’t be enclosed in angle brackets.
Without knowing exactly what you’re dealing with, we can’t really tell you how to get where you’re trying to go.
That said, assuming amounts
is an object and months
is an array, it’s pretty trivial. Using months.reduce
will let you build a different object entirely. Here’s the idea:
const finalArray = months.reduce( function(accumulator, month, index){
/***
* Here, you want to set the new value in your accumulator to a sub-array, containing month
* and the member of the amounts object with the property name that matches this index:
***/
accumulator[index] = [/* in here, set the values. Not giving away all the answers... */];
return accumulator;
}, []).unshift(["Month", "Amount"]); // this last bit will prepend the column names you want.
You could actually do much the same with map()
, as I think about it, but value[k]
is not doing much, if the amounts
array is named amounts
and not value
…
const finalArray = month.map( (mo, index) => [mo, amounts[index] ] ).unshift(["Month", "Amount"]);