jQuery is a very excellent JavaScript library that can easily manipulate and process HTML documents, handle events, dynamically change CSS and page content, and even perform Ajax operations. During the development process of jQuery, we often encounter the conversion of various data types. This article will introduce in detail the commonly used data type conversion methods in jQuery.
1. Number conversion
The parseInt method can convert the string type to the integer type, and can also specify the forward format:
var num = parseInt("123"); console.log(num); // 123 var num = parseInt("123.67"); console.log(num); // 123 var num = parseInt("0xfad", 16); console.log(num); // 4013
The parseFloat method can convert the string type to a floating point number type, and can also truncate or retain the specified number of digits:
var float = parseFloat("123.67"); console.log(float); // 123.67 var float = parseFloat("123.671284"); console.log(float.toFixed(2)); // 123.67
2. String conversion
The toString method can convert the numerical type to the string type, and can also specify the carry system:
var num = 123; console.log(num.toString()); // "123" var num = 10; console.log(num.toString(2)); // "1010"
join method can convert the array type to string type:
var array = [1, 2, 3, 4]; console.log(array.join()); // "1,2,3,4" console.log(array.join("-")); // "1-2-3-4"
3. Array conversion
toArray method can convert array-like objects into real array objects:
var args = function() { return arguments; }(); // 注意这里必须要加括号,否则会被当成语句块处理 var arr = Array.prototype.slice.call(args); console.log(arr); // [1, 2, "hello"]
from method can convert certain array-like objects into Convert the object to a real array object:
var set = new Set([1, 2, 3]); var arr = Array.from(set); console.log(arr); // [1,2,3]
4. Object conversion
The JSON.stringify method can convert any object Into JSON string:
var obj = { name: "Tom", age: 20 }; console.log(JSON.stringify(obj)); // {"name":"Tom","age":20}
$.param method can convert any object into a serialized string:
var obj = { name: "Tom", age: 20 }; console.log($.param(obj)); // "name=Tom&age=20"
5. Others
typeof method can determine the data type:
console.log(typeof 123); // "number" console.log(typeof "123"); // "string" console.log(typeof true); // "boolean" console.log(typeof null); // "object" console.log(typeof undefined); // "undefined" console.log(typeof {}); // "object" console.log(typeof []); // "object" console.log(typeof function() {}); // "function"
isNumeric method can determine whether a value is a number:
console.log($.isNumeric("123")); // true console.log($.isNumeric(123)); // true console.log($.isNumeric("1e5")); // true console.log($.isNumeric("1.23")); // true console.log($.isNumeric("0xF")); // true console.log($.isNumeric("hello")); // false
During the development process, we need to frequently convert data types. The methods mentioned above are all must be understood for development. Hope it helps everyone.
The above is the detailed content of jquery data type conversion. For more information, please follow other related articles on the PHP Chinese website!