Anyone know a good way to turn this?:
var obj = [{key1: value1,key2: value2},{key3: value3,key4: value4}];
into:
var obj = [{Key1: value1,Key2: value2},{Key3: value3,Key4: value4}];
Can you use objects as Object keys in JavaScript? # The short answer is "no". All JavaScript object keys are strings.
JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.
As of 2019 you can use Object.fromEntries
:
let populations = {london: 8.9, beijing: 21.54, mumbai: 18.41}; // March 2020
let entries = Object.entries(populations);
let capsEntries = entries.map((entry) => [entry[0][0].toUpperCase() + entry[0].slice(1), entry[1]]);
let capsPopulations = Object.fromEntries(capsEntries);
console.log(capsPopulations);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With