For example, I had an array with 3 numbers:
var arr = [124, -50, 24];
and I need to convert this array to the object:
{
x: 124,
y: -50,
z: 24
}
I don`t want to use "old-style" syntax for this, for example:
{
x: arr[0],
y: arr[1],
z: arr[2]
}
so for now, I`m using that syntax:
const [x, y, z] = [...arr];
const obj = {x, y, z};
But, is there is any way to do this with a straight dectructuring array to object without need of temporary variables?
var obj = (([x,y,z]) => ({x,y,z}))(arr);It remains only to understand what is going on there@VladPovalii it's an IIFE of the arrow function([x,y,z]) => ({x,y,z}). But imo you should store this functionconst arr2point = ([x,y,z]) => ({x,y,z});and use thatconst obj = arr2point(arr)instead of using the IIFE inline.