I would like to create a JS object made of specific array values. For example, I have an array ["bananas", 5] and I would like to convert it into an object {"bananas" : 5}. Could you please let me know how could I do that?
Asked
Active
Viewed 21 times
1
Konstantink1
- 445
- 1
- 3
- 16
1 Answers
2
Object.fromEntries can be used to create an object from an array of arrays:
let input = ["bananas", 5];
let output = Object.fromEntries([input]);
console.log(output);
Where each two-elements array represents a key-value pair from your new object.
mickl
- 44,970
- 9
- 50
- 76