简体   繁体   English

如何在javascript中使用此数组格式返回正确的值?

[英]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()我想在不更改'1''2'情况下获得正确的 id 值,因为如果我将'1''2'更改为'A' ,例如我可以简单地编写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>
    );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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