Skip to content

Instantly share code, notes, and snippets.

@aryankarim
Created June 12, 2022 06:42
Show Gist options
  • Save aryankarim/93b25b42d55ffa56984951da13159e10 to your computer and use it in GitHub Desktop.
Save aryankarim/93b25b42d55ffa56984951da13159e10 to your computer and use it in GitHub Desktop.
reformat array of objects in javascript
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