11

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?

Vlad Povalii
  • 1,559
  • 16
  • 23
  • 4
    off the top of my head `var obj = (([x,y,z]) => ({x,y,z}))(arr);` – Jaromanda X Sep 26 '17 at 12:35
  • https://stackoverflow.com/q/45971203/1048572? – Bergi Sep 26 '17 at 12:36
  • @Bergi, there's no array -> object in that question ... is there? – Jaromanda X Sep 26 '17 at 12:37
  • @JaromandaX It's about "renaming" properties, like from `0` to `x` - whether it's an array object or plain object doesn't make much difference, the choices for destructuring and literal syntax are the same. – Bergi Sep 26 '17 at 12:38
  • 2
    I think this is similar: https://stackoverflow.com/questions/38242744/destructure-array-to-object-property-keys – llama Sep 26 '17 at 12:39
  • ahhh yes @bergi, I see that now ... I think :p – Jaromanda X Sep 26 '17 at 12:41
  • @JaromandaX In any case, I answered rather than closing as a duplicate. It's not totally obvious indeed. – Bergi Sep 26 '17 at 12:42
  • @llama - similar? identical!!! – Jaromanda X Sep 26 '17 at 12:42
  • @JaromandaX thanks, that works! It remains only to understand what is going on there :) – Vlad Povalii Sep 26 '17 at 12:44
  • 1
    @JaromandaX: simidentical? identilar? – llama Sep 26 '17 at 12:45
  • @llama - yes you right! I missed that question :( But the best answer there is getting array values by indexes. I have mentioned in my question that is not what im looking for – Vlad Povalii Sep 26 '17 at 12:47
  • 1
    `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 function `const arr2point = ([x,y,z]) => ({x,y,z});` and use that `const obj = arr2point(arr)` instead of using the IIFE inline. – Thomas Sep 26 '17 at 12:51
  • Why do people always try to use destructuring to build an object ? :( – Felix Kling Sep 26 '17 at 14:43

3 Answers3

1

You can also do

const obj = {};
([obj.x, obj.y, obj.z] = arr);

to avoid the temporary variables, but I'd question whether that's an improvement.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

As it was already mentioned in the comment you can use an Immediately Invoked Function Expression (IIFE) to create the object in a single step, but it's less readable then multiple steps.

const arr = [124, -50, 24];
const obj = (([x, y, z]) => ({ x, y, z }))(arr);

console.log(obj);
Vlad Povalii
  • 1,559
  • 16
  • 23
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • lol. I've changed the sorting order by mistake, and this question popped. I've answered it like it was new (not really looked at the comments), and totally forgot about it. – Ori Drori May 28 '19 at 11:19
  • 1
    :) Anyway its ok..finally this question will have the right answer, so thanks! – Vlad Povalii May 28 '19 at 11:25
1

Just use this

let obj = {...arr}
Abdur Rahaman
  • 197
  • 2
  • 12
  • Not quite right, because you want to control the object keys naming ("x", "y", "z") - but after this keys will be: "0", "1", "2" – Vlad Povalii May 28 '19 at 11:28