Created
June 12, 2022 06:42
-
-
Save aryankarim/93b25b42d55ffa56984951da13159e10 to your computer and use it in GitHub Desktop.
reformat array of objects in javascript
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
function formatAsTextValue(list, { text, value }) { | |
return list.map((item) => ({ text: item[text], value: item[value] })); | |
} | |
formatAsTextValue( | |
[ | |
{ id: 1, name: "Adam" }, | |
{ id: 2, name: "Alonso" }, | |
], | |
{ text: "name", value: "id" } | |
); | |
// result: | |
// [ | |
// { value: 1, text: "Adam" }, | |
// { value: 2, text: "Alonso" }, | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment