JavaScript: Convert Object Keys from 'titleLable' to 'lable' and 'titleValue' to 'value'
This code snippet shows how to convert an object like {'titleLable': 1, 'titleValue': 2} to {'lable': 1, 'value': 2} in JavaScript:
const data = {'titleLable': 1, 'titleValue': 2};
const result = {};
for (const key in data) {
const newKey = key.replace('title', '');
result[newKey] = data[key];
}
console.log(result);
This code iterates through each key in the original object. For each key, it removes the 'title' prefix, creating a new key. The corresponding value is then assigned to the new key in the result object.
The output of this code will be:
{'lable': 1, 'value': 2}
原文地址: https://www.cveoy.top/t/topic/pjal 著作权归作者所有。请勿转载和采集!