How to remove parts of a field name in a serialized structure?

Asked

Viewed 20 times

1

I am selecting my form to then convert it to JSON, and I need to remove the string "PessoaViewModel." of all Fields. The serialized data looks like this:

"PessoaViewModel.Id=2&PessoaViewModel.PessoaNatureza=Juridica&PessoaViewModel.PessoaFisicaViewModel.PessoaId=2&"

Instead of staying "PessoaViewModel.Id=2", should stay "Id=2".

var pessoaViewModel = $("form :input")
.filter(function (index, element) {
    return $(element).val() != '';
})
.serialize();

Someone knows how to help me?

1 answer

0


I don’t know where this is coming from PessoaViewModel., but you can remove it with a replace using a regular expression. Just add .replace(/PessoaViewModel\./g, '') in serialize:

var pessoaViewModel = $("form :input")
.filter(function (index, element) {
    return $(element).val() != '';
})
.serialize()
.replace(/PessoaViewModel\./g, '');

Note that you need to escape the point with \., because the dot has a function of its own in regular expression.

  • I get it. Thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.