-2

I have an array like this:

const companyNames = ["A", "B", "C"];

I want to convert it to a something like this:

const companyNames = {
  0: 'A',
  1: 'B',
  2: 'C'
};
Ozan
  • 3
  • 1

2 Answers2

1

You can use Spread Operator:

{ ...["A", "B", "C"] }
Prashant Pimpale
  • 9,685
  • 7
  • 35
  • 75
demkovych
  • 5,854
  • 3
  • 16
  • 24
0

You can use forEach:

var companyNames = ["A", "B", "C"];
var data = {};
companyNames.forEach(function(value, index) {
  data[index] = value;
})
console.log(data)
Prashant Pimpale
  • 9,685
  • 7
  • 35
  • 75
Aishwarya
  • 515
  • 10
  • 21