Fred J. Fred J. - 4 months ago 12
Javascript Question

serializeArray() returning too many elements

Please see the image below, I needed

serializeArray()
to return one object and not 4 as the
length
property gave only one in the selection.

How can I get jQuery serializeArray() to just return all the elements which do not have the class
inactive
as stated in the image? Thanks

enter image description here

Answer

If requirement is to serialize a single element to an object within and array, you can create an array, and an object; set object property to name of element, set object value to value of element, push object to array.

var arr = [];
var obj = {};
var props = $("form").not(".inactive");
obj[props.attr("name")] = props.val();
arr.push(obj);
console.log(arr);