How does one convert camelCase to MACRO_CASE in JavaScript?
Asked
Active
Viewed 58 times
1 Answers
0
Converting an Object:
const test = {
testId: '123',
testString: 'whoa'
};
const testConverted = Object.assign({}, ...Object.keys(test).map((key, index) => ({[key.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase()]: Object.values(test)[index]})));
console.log(testConverted);
Converting a String:
const testString = 'convertThisToMacroCase';
const convertedString = testString.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase();
console.log(convertedString);

Noel Schenk
- 724
- 1
- 8
- 19