score:0
You can try the below code,
const renderField = () =>
this.props.total_calculation.length > 0 ? (
this.props.total_calculation.map(function (obj, key) {
return (
<td key={key} colSpan="2">
{obj['Year']}
</td>
);
})) : null;
score:1
Your problem might not be an optimization problem. What you are searching for is to have only one function that return the render for keys of your object. Here is a suggestion:
- Change you
renderYear()
method to this:
renderField(objectKey) {
if (this.props.total_calculation.length === 0) {
return null;
}
return this.props.total_calculation.map(function (obj, key) {
return (
<td key={key} colSpan="2">
{obj[objectKey]}
</td>
);
});
}
The argument objectKey
of the renderField()
method takes as argument a string that is a key of the object you want to render. Then now you can call it like this:
<tr className='year'>
<td>Year</td>
{{this.renderField('Year')}}
</tr>
You can call the renderField()
method on other key of your object like 'rent_increase'
You should also check the Open/closed principle of the SOLID principles because your problem has similar solution to it.
Source: stackoverflow.com
Related Query
- Use map function for array object and display in table in React
- Convert object to array with Lodash _.map and use map function in React
- How to map over 2 Array of object in React and render to Table
- Not able to set the JSON object of Arrays in React js using axios.get function and display the object of Arrays in table
- How Use images from javascript array in react using filter and map function
- React push object into array and map error map not a function
- How to arrange nested array of object data for react table component display
- Problem with use of map function in react for setting text and color of buttons
- React map object and embedded object from JSON and convert to single array for state
- I want to map my array from an object using react and next.js, it's a function component using hooks
- 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 can I use Map function for an Array with values and another array as a value in Javascript
- how to map an array of images and use it as inline style in React
- In React Hooks. How to set state to a random number and use that to display a string from an array
- how should we convert string data into array and display into react table
- React Formik : how to use custom function onChange and onConfirmChange for request input
- how do i update state in react for updating the value of an object inside an array using array.map function
- display array data through map in react table
- Warning: Unexpected ref object provided for input. Use either a ref-setter function or React.createRef(). With React Hook Form
- How can use my object like a array using with map function
- How do I map an through an array of objects and display each object individually? I want each pizza order to show
- React Context: How do I pass an object array and display contents in Consumer?
- Get my arrow function output and display it in a table format in render component in React
- JS map array to return object of arrays for React
- JavaScript: How to map object with nested arrays to table for React
- How to use a state value with map function to display repeat ui in react component
- Array map function in react for ternary operator
- Repeat an array object with key and values with map in react js
- Retrieve row value selected in a Table as an array and send the results to another function on React
- Error: Objects are not valid as a React child when trying to map a nested array for a table component in React
More Query from same tag
- Make button invisible onClick and show another button
- react-chartjs error Cannot read property 'Chart' of undefined
- How to use history using withRouter outside component
- Get data from server and pass values to sub components in React (useEffect, useFetch..)
- How to move navbar to right and apply background color?
- react toggle and close when clicked at other element/body failed
- REACT - Composition of a component with subcomponents in TypeScript
- img tag in reactjs is not displaying on render
- Create-React-App failed to compile | Import/First Error
- Can I use <%= tag %> in React-Rails?
- Collection.find() very slow with other BDD Meteor React
- There is getting a error when i am filtering data with array
- How would you group a field in a table so it doesnt show duplicates - react
- Redux Toolkit RTK Query sending query parameters is returning undefined
- yield is a reserved keyword error inside async function
- React: Don't render components in table who aren't visible
- How can I reroute to a new page if a POST operation is successful?
- How to check in Next.js if a dynamic path does not exists and to return 404 after checking in database?
- Redux not saving state
- Why map() function doesn't work in my react app?
- React update state in a new form
- JavaScript String to Array and add boolean
- Why is margin top is not working in Tailwind CSS?
- tree-shaking a big object
- How to fix "react-dom.development.js:287 Uncaught TypeError: Cannot read property 'setState' of undefined"
- Laravel 7 + React render error: Nothing was returned from render
- Local storage manipulation
- Broken file upload with React to AWS S3
- How to add "outside" event listener to Markers in Google Maps (react-google-maps/api)
- How to show the selected row in the modal