Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Array.from() to transform Api data to conform to a React component.
// MDN Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
const apiCategories = [
{ id: 0, title: "javascript", description: "...", other: "..." },
{ id: 1, title: "React", description: "...", other: "..." }
];
const transformApiCategories = () => {
return Array.from(apiCategories, category => {
return {label: category.title, value: category.id};
});
};
console.log(transformApiCategories());
// [0: Object {label: "javascript" value: 0}
// 1: Object {label: "React" value: 1}]
// Example use in a react select component.
return (<SelectControl options={ transformApiCategories() }/>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.