score:214
The problem is the way you're using forEach()
, as it will always return undefined
. You're probably looking for the map()
method, which returns a new array:
var tifOptions = Object.keys(tifs).map(function(key) {
return <option value={key}>{tifs[key]}</option>
});
If you still want to use forEach()
, you'd have to do something like this:
var tifOptions = [];
Object.keys(tifs).forEach(function(key) {
tifOptions.push(<option value={key}>{tifs[key]}</option>);
});
Update:
If you're writing ES6, you can accomplish the same thing a bit neater using an arrow function:
const tifOptions = Object.keys(tifs).map(key =>
<option value={key}>{tifs[key]}</option>
)
Here's a fiddle showing all options mentioned above: https://jsfiddle.net/fs7sagep/
score:-9
I highly suggest you to use an array instead of an object if you're doing react itteration, this is a syntax I use it ofen.
const rooms = this.state.array.map((e, i) =>(<div key={i}>{e}</div>))
To use the element, just place {rooms}
in your jsx.
Where e=elements of the arrays and i=index of the element. Read more here. If your looking for itteration, this is the way to do it.
score:3
You can use map
function
{Object.keys(tifs).map(key => (
<option value={key}>{tifs[key]}</option>
))}
score:4
const tifOptions = [];
for (const [key, value] of Object.entries(tifs)) {
tifOptions.push(<option value={key} key={key}>{value}</option>);
}
return (
<select id="tif" name="tif" onChange={this.handleChange}>
{ tifOptions }
</select>
)
score:5
you could also just have a return div like the one below and use the built in template literals of Javascript :
const tifs = {1: 'Joe', 2: 'Jane'};
return(
<div>
{Object.keys(tifOptions).map((key)=>(
<p>{paragraphs[`${key}`]}</p>
))}
</div>
)
score:8
When I use Object.entries
with map
, I use Array Destructuring like the following and just call them directly.
Object.entries(tifs).map(([key, value]) => (
<div key={key}>{value}</div>
));
score:30
You can use it in a more compact way as:
const tifs = {1: 'Joe', 2: 'Jane'};
...
return (
<select id="tif" name="tif" onChange={this.handleChange}>
{ Object.entries(tifs).map((t,k) => <option key={k} value={t[0]}>{t[1]}</option>) }
</select>
)
And another slightly different flavour:
Object.entries(tifs).map(([key,value],i) => <option key={i} value={key}>{value}</option>)
Source: stackoverflow.com
Related Query
- React - How to loop and update values in object model?
- React Object of Objects - How to loop through
- How to loop an object that is inside an array and is inside an object? React
- How can I loop over a JSON object in React
- How to loop an object in React
- how to loop throgh array of object from axios json respone in react js and set them in div?
- How to loop through an array and display data within the object of that array, in react and javascript
- How to loop through complex object using Map in React
- How to use for loop in react so I can loop over two array and return the object in second array if both match
- How to loop an object in React?
- How do I update states `onChange` in an array of object in React Hooks
- How can I remove an attribute from a React component's state object
- How to loop over a number in React inside JSX
- React hooks: How do I update state on a nested object with useState()?
- how to create React search filter for search multiple object key values
- How import object from external JS file in React JS using web pack
- How to update object in React state
- how to access history object in new React Router v4
- How to set React default props for object type
- How to loop through object in JSX using React.js
- How to convert object to string in react js
- How do I pass an object containing props to React component?
- How to access the class name of a React object in this.props.children
- How to edit particular object in React
- How to access Object array file into react component?
- React Native - LayoutAnimation: how to make it just animate object inside component, not whole component/view?
- React How to fix Failed prop type - Invalid prop of type string expected object
- How to delete object from array using object property - React
- In React how to add key prop to element passed as Object
- How to loop inside map function using jsx format React JS
More Query from same tag
- RangeError: invalid array length
- How to use .env variables inside cypress.json file?
- maintaining state between two components in a js package
- I got err `Cannot find module 'react' from 'makeStyles.js' ` when I import a component in the test file
- Onblur is automatically called on OnFocus event in React
- Possible to transfer state/props data from server
- How to set PUBLIC_URL back to the server root for npm run build
- how to display elements when I click on it?
- Using useRef and useEffect to store to variables once on mount with functional component
- Try to hide header on login page but did not work
- firebase not return the key?
- How to preload images in React.js?
- Drawer in React.js
- How can I map over my data set and set a single variable to an objects name?
- How can I create multiple routes using React Router v4 (Sidebar)?
- Avoid recalculating variable on every render in React
- how to access the data in the pie?
- How to run temporary effect when data changes in React?
- How to call react-modal from rails-assets.org in react-rails component
- Package that is linked with npm link doesn't update
- Problem while embedding code in Contentful CMS
- React backgroundImage referenced in JSON file path seem weird
- yii2 restful api: (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
- Background event on Highcharts Gantt
- Blank screen React-Leaflet
- Initialize props from Store
- Material UI global css variables
- Not able to update the selected option in material UI dropdown
- How can I display data from a server in a material-table?
- Using two useeffect react hooks