本文最后更新于 2024-10-17T11:18:40+08:00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
function convertKeysToNumbers(obj, ...keys) { return keys.reduce((newObj, key) => { if (obj.hasOwnProperty(key)) { const value = parseFloat(obj[key]); newObj[key] = isNaN(value) ? obj[key] : value; } return newObj; }, { ...obj }); }
const originalObj = { a: '123', b: 'abc', c: '45.67', d: '8e2', e: 'Not a Number' };
const result = convertKeysToNumbers(originalObj, 'a', 'c', 'd', 'e'); console.log(result);
|
js实现将对象中指定的字段转换为数字类型
https://mengluo.com/2024/08/22/js实现将对象中指定的字段转换为数字类型/