简体   繁体   中英

How to return the correct values at with this array format in javascript?

I have this array like this:

const tab = [
  { '1': {'id':'me'}},
  { '2': {'id':'you'}}
];

and I have created this function to get values from the last array like this:

 const ar = tab.map((user,i)=>{
  return(
      <div>
      <p>{tab[i][1].id.toString()}</p>
      </div>
    )
     });

Note:

I want to get the right values of the id without changing '1' or '2' because if I changed the '1' or '2' to 'A' for example I can simply write tab[i].A.id.toString()

Something like this might work for you:

const tab = [
    { '1': { id: "me" }},
    { '2': { id: "you" }}
];

const ar = tab.map((user, i) => {
    return (
        <div>
            <p>{user[(i + 1).toString()].id}</p>
        </div>
    );
});

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