How to convert this array to string separated by commas with jquery?
var array = $('#form').serializeArray();
Thanks!
How to convert this array to string separated by commas with jquery?
var array = $('#form').serializeArray();
Thanks!
- by using
$.map()
doc- demo https://codepen.io/luca-filosofi/pen/GRjKYYa
$(function () {
var ATTRIBUTE_SEPARATOR = ":";
var PROPERTY_SEPARATOR = ", ";
// EXAMPLE 1
var serialize_array = $("#form").serializeArray();
console.log(serialize_array);
var converted_string_1 = $.map(serialize_array, function (prop, i) {
return prop.name + ATTRIBUTE_SEPARATOR + prop.value;
}).join(PROPERTY_SEPARATOR);
console.log(converted_string_1);
// a:a, b:b, c:c, d[]:d1, d[]:d2
// EXAMPLE 2
var serialized_string = $("#form").serialize();
var converted_string_2 = decodeURIComponent(
serialized_string
.replace(/\&/g, PROPERTY_SEPARATOR)
.replace(/\=/g, ATTRIBUTE_SEPARATOR)
);
console.log(converted_string_2);
// a:a, b:b, c:c, d[]:d1, d[]:d2
// EXAMPLE 3
var converted_string_3 = JSON.stringify(serialize_array);
console.log(converted_string_3);
// [{"name":"a","value":"a"},{"name":"b","value":"b"},{"name":"c","value":"c"},{"name":"d[]","value":"d1"},{"name":"d[]","value":"d2"}]
});
You can do it like this:
Or