Created
August 19, 2019 09:33
-
-
Save fxmontigny/c429c44aafdd9105d9b317dddb560802 to your computer and use it in GitHub Desktop.
Get json from Array of objects
This file contains 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
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