Để sửa đổi các giá trị khóa, bạn cần sử dụng biểu thức chính quy cùng vớiObject.fromEntries ().
Ví dụ
var underscoreSpecifyFormat = str => str.replace(/(_)(.)/g, (_, __, v)
=> v.toUpperCase()),
JsonObject = { first_Name_Field: 'John', last_Name_Field: 'Smith'},
output = Object.fromEntries(Object.entries(JsonObject).map(([key, value]) => [underscoreSpecifyFormat(key), value]));
console.log("The JSON Object=");
console.log(output); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo89.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\JavaScript-code> node demo89.js
The JSON Object=
{ firstNameField: 'John', lastNameField: 'Smith' }