-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
  • 10,349
  • 9
  • 44
  • 84
demkovych
  • 7,827
  • 3
  • 19
  • 25
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
  • 10,349
  • 9
  • 44
  • 84
Aishwarya
  • 637
  • 9
  • 21