I am curious if it is possible to check if the object value exists and if it exists I want to check if it is a string or an array
.
// var n is a dynamic variable that is retrieved from $(this)
var d = {};
if(typeof(d[n]) == "undefined"){
d[n] = "value";
}else{
if(typeof(d[n]) == "string"){
//Convert string to array using its curent value plus an additional value
}else{
//Append value to array
}
}
Currently I am using a loop to create an object
for AJAX. I have a few of the same names for the data points similar to an input with the name="name[]"
.
How can I accomplish this?
All you need to do is reassign the property to an array if a string ( d[n] = [d[n], newValue]
), or call Array#push if it's not.
var newValue = "testing123";
var d = {};
if(typeof d[n] == "undefined"){
d[n] = "value";
}else{
if(typeof d[n] == "string"){
//Convert string to array using its current value plus an additional value
d[n] = [d[n], newValue];
}else{
//Append value to array
d[n].push(newValue);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.