Created
June 27, 2017 21:11
-
-
Save dmgig/68c3cf22a5649f6443df7a2eacb4f241 to your computer and use it in GitHub Desktop.
jQuery serializeArray Change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * take jQuery serializedArray output, and convert the elements which | |
| * should be arrays (ie fieldnames[]) and make them arrays | |
| */ | |
| var fixSerializedArray = function(arr){ | |
| var workObj = {}; | |
| var newArr = []; | |
| for(var i in arr){ | |
| if(workObj.hasOwnProperty(arr[i].name)){ | |
| console.log(typeof workObj[arr[i].name]); | |
| if(!Array.isArray(workObj[arr[i].name])){ | |
| workObj[arr[i].name] = [ workObj[arr[i].name] ]; | |
| workObj[arr[i].name].push(arr[i].value); | |
| }else{ | |
| workObj[arr[i].name].push(arr[i].value); | |
| } | |
| }else{ | |
| workObj[arr[i].name] = arr[i].value; | |
| } | |
| } | |
| for(var key in workObj){ | |
| if (workObj.hasOwnProperty(key)) { | |
| newArr.push({ 'name': key, 'value': workObj[key] }); | |
| } | |
| } | |
| return newArr; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment