Last active
June 14, 2020 13:47
-
-
Save vanaf1979/57b075efcd0c2ff7a80588113bf01184 to your computer and use it in GitHub Desktop.
Use Array.from() to transform Api data to conform to a React component.
This file contains hidden or 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
| // 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