Created
September 23, 2016 02:23
-
-
Save philpoore/3cef8011e7083fc19ad614a3856824ac to your computer and use it in GitHub Desktop.
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
// manage json objects to js form data | |
const formData = (obj) => { | |
let data = new FormData(); | |
for (var key in obj){ | |
if (!obj.hasOwnProperty(key)) { continue; } | |
let item = obj[key]; | |
if (typeof item === 'undefined'){ continue; } | |
if (typeof item === 'object' && item.length){ | |
for (var i = 0; i < item.length; i += 1){ | |
data.append(key + '[]', item[i]); | |
} | |
continue; | |
} | |
if (typeof item === 'object'){ | |
for (var item_key in item){ | |
if (!obj.hasOwnProperty(key)) { continue; } | |
data.append(key + '_' + item_key, item[item_key]); | |
} | |
continue; | |
} | |
data.append(key, item); | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment