Boolean转为int型
程序员文章站
2024-03-18 21:20:34
...
Boolean转为int型
字符串类型
//将true和false转为1和0
function dealBoolean(obj) {
if (obj === false) {
obj = 0;
} else if (obj === true) {
obj = 1;
}
return obj
}
对象类型
function dealBooleanObj(obj) {
var key = null;
for (key in obj) {
if (typeof obj[key] === 'object' &&
!(obj[key] instanceof Array)) {
dealBoolean(obj[key]);
} else if (obj[key] === false) {
obj[key] = 0;
} else if (obj[key] === true) {
obj[key] = 1;
} else if (obj[key] === 'undefined') {
obj[key] = '';
}
}
}
数组类型
function dealBooleanArr(arr) {
for(var i=0,len=arr.length;i<len;i++){
var key = null;
var obj = arr[i];
for (key in obj) {
if (typeof obj[key] === 'object' &&
!(obj[key] instanceof Array)) {
dealBoolean(obj[key]);
} else if (obj[key] === false || obj[key] == "false") {
obj[key] = 0;
} else if (obj[key] === true || obj[key] == "true") {
obj[key] = 1;
} else if (obj[key] === 'undefined') {
obj[key] = '';
}
}
}