How to render a Object in React?

后端 未结 4 862
忘了有多久
忘了有多久 2021-02-14 09:16

I want to render a Object .

My Object is:

const ObjectTest = {
        1: {
            id : 1,
            name:\'ABC\'
                 


        
相关标签:
4条回答
  • 2021-02-14 09:25

    Data is an object so directly we can't use map on that, so use Object.keys or Object.entries to get the array first, then use map on that to create ui items.

    Using Object.keys:

    _renderObject(){
        return Object.keys(ObjectTest).map(obj, i) => {
            return (
                <div>
                    id is: {ObjectTest[obj].id} ;
                    name is: {ObjectTest[obj].name}
                </div>
            )
        })
    }
    

    Using Object.entries:

    const ObjectTest = {
        1: {
            id : 1,
            name:'ABC'
        },
        3: {
            id: 3,
            name:'DEF'
        }
    }
    
    class App extends React.Component{
    
    	_renderObject(){
    		return Object.entries(ObjectTest).map(([key, value], i) => {
    			return (
    				<div key={key}>
    					id is: {value.id} ;
    					name is: {value.name}
    				</div>
    			)
    		})
    	}
    
    	render(){
    		return(
    			<div>
    				{this._renderObject()}
    			</div>
    		)
    	}
    }
    
    ReactDOM.render(<App/>, document.getElementById('app'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    
    <div id='app'/>

    Check this answer also: How to loop an object in ReactJS?

    0 讨论(0)
  • 2021-02-14 09:42

    In react you can render your wish answer in browser screen by below code

    import React from 'react';
    
    
    const ObjectTest = {
      1: {
          id : 1,
          name:'ABC'
      },
      3: {
          id: 3,
          name:'DEF'
      }
    }
    
    class App extends React.Component {
    
       constructor(props) {
          super(props);
       };
    
       render() {
          return (
             <div>
             {
                Object.keys(ObjectTest).map((value,index)=>{
                   <p>id is {ObjectTest[value].id} ; name is {ObjectTest[value].name}</p>
                });
             }
             </div>
          );
       }
    }
    
    export default App;
    
    0 讨论(0)
  • 2021-02-14 09:42

    I wonder why it seems that almost every one is using Object.keys!

    Object.values(ObjectTest).map((row_, index) =>
        <tr>
        {Object.values(row_).map(item => <td>{item}</td>)}
        </tr>
    )}

    Sample Data:

    const ObjectTest = {
            1: {
                id : 1,
                name:'ABC'
            },
            3: {
                id: 3,
                name:'DEF'
            }
    }

    0 讨论(0)
  • 2021-02-14 09:44

    const ObjectTest = {
            1: {
                id : 1,
                name:'ABC'
            },
            3: {
                id: 3,
                name:'DEF'
            }
    }
    render(){
       return (
             <div>
                Object.keys(ObjectTest).map( (key)=> {
                  return <div>
                     <span>"Id is:"{key}<span>
                     <span>"Name is:"{ObjectTest[key].name}<span>
                  </div>
                })
             </div>
       )
    }

    0 讨论(0)
提交回复
热议问题