So I'm working with this function:
export default function getObjectFromTwoArrays(keyArr: Array<any>, valueArr: Array<any>) {
// Beginning point:
// [key1,key2,key3],
// [value1,value2,value3]
//
// End point: {
// key1: value1,
// key2: value2,
// key3: value3
// }
if (keyArr.length === valueArr.length) {
// @ts-ignore
return Object.assign(...keyArr.map((el,index) => ({[el]: valueArr[index]})))
} else {
throw new Error(`The arrays must be the same length: ${{keyArr}}, ${{valueArr}}`)
}
}
I am trying to achieve the desired result from the example above - End point. I attempted to assign it to a variable, but encountered the same issue.
I also tried replacing it with:
Object.assign({...keyArr.map((el,index) => ({[el]: valueArr[index]}))})
However, the outcome would be:
{0: {key1: value1}, 1: {key2: value2}}
The same goes for
Object.assign([...keyArr.map((el,index) => ({[el]: valueArr[index]}))])
Resulting in: [{key1: value1}, {key2: value2}]