-1

I have this array: ["description", "asc"]

and want to turn it into this object:

{ "description": "asc" }

What's the fastest way to achieve this?

I tried

const obj =  { array[0] : array[1] }

But it doesn't work because I can't seem to set the key of the object dynamically?

nburk
  • 22,409
  • 18
  • 87
  • 132

3 Answers3

1

you can try this

const array = new Map([
  ["description", "asc"]
]);

const convobj = Object.fromEntries(array);

console.log(convobj)
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
0

You can do this:

let array = ["description", "asc"];
const obj = { [array[0]]: array[1] };
console.log(obj);
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
0

You can try this:

const bar = {
   [array[0]]: array[1]
}
Tyler
  • 1,163
  • 16
  • 37