Dùng Object.fromEntries(), Object.entries(), map() và Regex để chuyển key từ snake_case sang camelCase.
Ví dụ
const toCamelCase = str => str.replace(/(_)(.)/g, (_, __, char) => char.toUpperCase());
const jsonObject = {
first_Name_Field: 'John',
last_Name_Field: 'Smith'
};
const output = Object.fromEntries(
Object.entries(jsonObject).map(([key, value]) => [toCamelCase(key), value])
);
console.log("Kết quả:", output);
Cách chạy
node demo.js
Kết quả
Kết quả: { firstNameField: 'John', lastNameField: 'Smith' }
Regex: /(_)(.)/g khớp gạch dưới + ký tự tiếp theo, thay bằng ký tự đó viết hoa.