JavaScript中的数据类型包括字符串、数字、布尔、数组、对象等,以下是它们之间互相转换的函数:
字符串转数字:
数字转字符串:
字符串转布尔:
布尔转字符串:
数组转字符串:
字符串转数组:
数字转布尔:
布尔转数字:
对象转字符串:
字符串转对象:
以下是每种数据类型互相转换的函数的实例说明:
let str = "123";
let num = parseInt(str);
console.log(num); // Output: 123
let str2 = "abc123";
let num2 = parseInt(str2);
console.log(num2); // Output: NaN
parseFloat()函数:
let str = "3.14";
let num = parseFloat(str);
console.log(num); // Output: 3.14
let str2 = "abc3.14";
let num2 = parseFloat(str2);
console.log(num2); // Output: NaN
Number()函数:
let str = "123";
let num = Number(str);
console.log(num); // Output: 123
let str2 = "abc123";
let num2 = Number(str2);
console.log(num2); // Output: NaN
let num = 123;
let str = num.toString();
console.log(str); // Output: "123"
String()函数:
let num = 123;
let str = String(num);
console.log(str); // Output: "123"
let str = "";
let bool = Boolean(str);
console.log(bool); // Output: false
let str2 = "abc";
let bool2 = Boolean(str2);
console.log(bool2); // Output: true
let bool = true;
let str = bool.toString();
console.log(str); // Output: "true"
String()函数:
let bool = false;
let str = String(bool);
console.log(str); // Output: "false"
let arr = [1, 2, 3];
let str = arr.toString();
console.log(str); // Output: "1,2,3"
join()方法:
let arr = [1, 2, 3];
let str = arr.join("-");
console.log(str); // Output: "1-2-3"
let str = "1,2,3";
let arr = str.split(",");
console.log(arr); // Output: [1, 2, 3]
let num = 0;
let bool = Boolean(num);
console.log(bool); // Output: false
let num2 = 1;
let bool2 = Boolean(num2);
console.log(bool2); // Output: true
布尔转数字:
Number()函数:
let bool = true;
let num = Number(bool);
console.log(num); // Output: 1
let bool2 = false;
let num2 = Number(bool2);
console.log(num2); // Output: 0
let obj = {name: "Tom", age: 18};
let str = ON.stringify(obj);
console.log(str); // Output: "{\"name\":\"Tom\",\"age\":18}"
let str = "{\"name\":\"Tom\",\"age\":18
以下是各种数据类型互相转换的函数封装在一个类中的代码示例:
class DataTypeConverter {
// 字符串转数字
static parseToNumber(str) {
return parseFloat(str);
}
// 数字转字符串
static numberToString(num) {
return num.toString();
}
// 字符串转布尔
static stringToBoolean(str) {
return Boolean(str);
}
// 布尔转字符串
static booleanToString(bool) {
return bool.toString();
}
// 数组转字符串
static arrayToString(arr, separator) {
return arr.join(separator);
}
// 字符串转数组
static stringToArray(str, separator) {
return str.split(separator);
}
// 数字转布尔
static numberToBoolean(num) {
return Boolean(num);
}
// 布尔转数字
static booleanToNumber(bool) {
return Number(bool);
}
// 对象转字符串
static objectToString(obj) {
return JSON.stringify(obj);
}
// 字符串转对象
static stringToObject(str) {
return JSON.parse(str);
}
}
使用示例:
let str = "123";
let num = DataTypeConverter.parseToNumber(str);
console.log(num); // Output: 123
let num2 = 123;
let str2 = DataTypeConverter.numberToString(num2);
console.log(str2); // Output: "123"
let str3 = "true";
let bool = DataTypeConverter.stringToBoolean(str3);
console.log(bool); // Output: true
let bool2 = false;
let str4 = DataTypeConverter.booleanToString(bool2);
console.log(str4); // Output: "false"
let arr = [1, 2, 3];
let str5 = DataTypeConverter.arrayToString(arr, "-");
console.log(str5); // Output: "1-2-3"
let str6 = "1,2,3";
let arr2 = DataTypeConverter.stringToArray(str6, ",");
console.log(arr2); // Output: [1, 2, 3]
let num3 = 0;
let bool3 = DataTypeConverter.numberToBoolean(num3);
console.log(bool3); // Output: false
let bool4 = true;
let num4 = DataTypeConverter.booleanToNumber(bool4);
console.log(num4); // Output: 1
let obj = {name: "Tom", age: 18};
let str7 = DataTypeConverter.objectToString(obj);
console.log(str7); // Output: "{\"name\":\"Tom\",\"age\":18}"
let str8 = "{\"name\":\"Tom\",\"age\":18}";
let obj2 = DataTypeConverter.stringToObject(str8);
console.log(obj2); // Output: {name: "Tom", age: 18}