Bada
Bada's Coding

Bada's Coding

Computed Property Names: All the Keys Change into String

Bada's photo
Bada
·Sep 16, 2022·

1 min read

Below is the example.

// string
const o1 = {};
o1['k1'] = 'v1';
const o1k1 = Object.keys(o1)[0];
console.log('o1k1', o1k1, typeof o1k1); // o1k1 k1 string

// number
const o2 = {};
o2[1] = 'v1';
const o2k1 = Object.keys(o2)[0];
console.log('o2k1', o2k1, typeof o2k1); // o2k1 1 string

// boolean
const o3 = {};
o3[true] = 'v1';
const o3k1 = Object.keys(o3)[0];
console.log('o3k1', o3k1, typeof o3k1); // o3k1 true string

// array
const o4 = {};
o4[['A', 'B']] = 'v1';
const o4k1 = Object.keys(o4)[0];
console.log('o4k1', o4k1, typeof o4k1); // o4k1 A,B string

// object
const o5 = {};
o5[{ k1: 'v1' }] = 'v1';
const o5k1 = Object.keys(o5)[0];
console.log('o5k1', o5k1, typeof o5k1); // o5k1 [object Object] string
 
Share this