更新时间:2021-09-12 05:50:35
这就是我最终解决它的方式.除了将变量类型添加到变量之外,我没有找到其他解决方案...
That's how I finally solved it. I didn't find any other solution than to add the variable type to the variable ...
var obj = {
a: '2',
b: '2.1',
c: '2.0',
d: 'text'
};
// Explicitly remember the variable type
for (key in obj) {
var value = obj[key], type;
if ( isNaN(value) || value === "" ) {
type = "string";
}
else {
if (value.indexOf(".") === -1) {
type = "integer";
}
else {
type = "float";
}
value = +value; // Convert string to number
}
obj[key] = {
value: value,
type: type
};
}
document.write("<pre>" + JSON.stringify(obj, 0, 4) + "</pre>");