javascript - How to convert an array [] to a series of { key: value } paired objects?
问题描述
I'm looking to transform this...
[
[ 'market', 'type', 'timeframe', 'proximal', 'distal', 'risk', 'tradeable' ], // object keys
[ 'AD', 'DZ', 'daily', '0.6375', '0.6283', '$920.00', 'FALSE' ] // key values
]
into this...
[
{
market: 'AD',
type : 'DZ',
timeframe: 'daily',
proximal: '0.6375',
distal: '0.6283',
risk: '$920.00',
tradeable: 'FALSE'
}
]
So far, here's what I have...
// each element of the array (I call a 'zone')
data.forEach(z => {
// each element inside the 'zone'
z.forEach(e => {
// headings to object keys, where k = key and v = value
const obj = headings.reduce((k,v) => (k[v]="",k),{})
console.log(obj)
})
})
console.log(obj) outputs this to the console:
Object {
distal: "",
market: "",
proximal: "",
risk: "",
timeframe: "",
tradeable: "",
type: ""
}
I just cant figure out how to get the values into those key pairs, PLEASE HELP!!
Thanks in advance,
Sam
解决方案
Try using it like this:
function reinterpret(data) {
let result = {}
data[0].forEach((key, index) => {
result[key] = data[1][index];
});
return [result];
}