Skip to content

Instantly share code, notes, and snippets.

@fxmontigny
Created August 19, 2019 09:33
Show Gist options
  • Save fxmontigny/c429c44aafdd9105d9b317dddb560802 to your computer and use it in GitHub Desktop.
Save fxmontigny/c429c44aafdd9105d9b317dddb560802 to your computer and use it in GitHub Desktop.
Get json from Array of objects
Array.prototype.toJson = function(name, value = name) {
return this.reduce((acc, cur) => {
acc[cur[name]] = cur[value];
return acc;
}, {});
}
/** Test **/
const form = [
{ name: 'firstname', flex: 1, value: 'test1' },
{ name: 'lastname', flex: 1, value: 'test2' },
{ name: 'email' , value: 'test3'},
{ name: 'message' },
];
console.log(form.toJson('name', 'value'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment