JSFiddle - React, Tailwind, and code Playground
HTML
<div id="console"></div>
JavaScript
var params = {
obj1: {
key1:'val1',
key2:'val2'
},
obj2: {
key1:'val1',
key2: {
nest1:'val1',
nest2:'val2',
nest3:'val3'}
},
obj3: {
key1:'val1',
key2: {
nest1: {
prop1: 'test',
prop2: 'test2',
}
}
}
}
var myFlatArray = [];
testParam(params, false, function( key, value ) {
value = $.isFunction( value ) ? value() : value;
myFlatArray.push({ name: key, value: value });
});
$.each(myFlatArray, function() {
$('#console').append($(this)[0].name
+ ' ' + $(this)[0].value+ '<br/>');
// We could create inputs and attach them to a form
// in order to Post it
});
// Serialize an array of form elements or a set of
// key/values into a query string
// Same as Jquery param function
function testParam( a, traditional, addFunction ) {
var r20 = /%20/g,
rbracket = /\[\]$/;
var s = [];
if (addFunction === undefined) {
var add = function( key, value ) {
// If value is a function, invoke it and return its value
value = $.isFunction( value ) ? value() : value;
s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
};
} else if ($.isFunction( addFunction )) {
var add = addFunction;
}
// Set traditional to true for jQuery <= 1.3.2 behavior.
if ( traditional === undefined ) {
traditional = $.ajaxSettings.traditional;
}
// If an array was passed in, assume that it is an array of form elements.
if ( $.isArray( a ) || ( a.$ && !$.isPlainObject( a ) ) ) {
// Serialize the form elements
$.each( a, function() {
add( this.name, this.value );
} );
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for ( var...