Reputation: 2714
So I have dozens of input fields and need to parse each of them into a object with key/value pairs. I can do that easily using this here
var myobj = {};
$(this).find('.stuff').each(function (e) {
var key = $(this).attr('name');
var val = $(this).val();
myobj[key] = val;
}).promise().done(function (e) {
// do more stuff
});
However, each value is parsed as string but some of them are int and some of them are decimals. How can I parse them automatically? Do I need to add e.g. a class to each input and parse them using e.g. switch
?
Upvotes: 0
Views: 106
Reputation:
You could try this:
if(!isNaN(val)) {
val = Number(val);
}
Try placing that under the line
var val = $(this).val();
Upvotes: 1