0 votes
in JavaScript by
What is the easiest way to convert an array to an object in javascript?

1 Answer

0 votes
by

You can convert an array to an object with the same data using spread(...) operator.

var fruits = ["banana", "apple", "orange", "watermelon"];
var fruitsObject = { ...fruits };
console.log(fruitsObject); // {0: "banana", 1: "apple", 2: "orange", 3: "watermelon"}
...