简体   繁体   中英

how do i access the required data using map and key

I have two names in the database and i want to display only the second name from it.If i give index=0 or index=1 it is giving me only the first name.So please tell me how do i retrieve the second name. And also it is retrieving the data from the database and displays it as separate un-ordered list. But i want to display all lists inside one un-ordered list.

   {this.state.post.map(function(item, index) {
     return (
    <ul key={index}>
    <li><Link className="prelink"  to="/namess">{item.Name}</Link>

    </ul>

JSON data

[ { _id: 5891ccdf0b50a85887a69ead,
    Id: 76,
    Age: 24,
    Name: ' SURYA',
    },
  { _id: 5892c76deebf7423e42da03b,
    Id: 77,
    Age: 24,
    Name: ' Vijay',
     } ]

You can access the second Name from JSON data as follows

 var data = [ { _id: '5891ccdf0b50a85887a69ead', Id: 76, Age: 24, Name: 'SURYA', }, { _id: '5892c76deebf7423e42da03b', Id: 77, Age: 24, Name: ' Vijay', } ] console.log(data[1].Name); 

Also if you want to display everything as one unordered list instread of separate for each one, you nee to place the <ul> tag outside the map function like

 <ul>{this.state.post.map(function(item, index) {
     return (
        <li key={index}><Link className="prelink"  to="/namess">{item.Name}</Link></li>
     )
     })
  }</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM