[英]How to exclude key in object with map
I have an object我有一个 object
{
id: 10,
email: "test@gmail.com",
roles: Array(1),
firstName: "toto",
lastName: "tata"
}
I would like to display only test@gmail.com,
toto
, tata
and exclude id
and role
我只想显示
test@gmail.com,
toto
, tata
并排除id
和role
So I did this but I don't know how to exclude id and role.所以我这样做了,但我不知道如何排除 id 和角色。 I tried with filter but it's not working.. Does anyone know how to do that?
我尝试使用过滤器但它不起作用..有谁知道该怎么做?
const datas = Object.entries(data).map(([key, value]) => {
return (
<div key={key.id}>
<p>
{value}
</p>
</div>
);
});
Use filter
to filter out the ones you dont want:使用
filter
过滤掉你不想要的那些:
const datas = Object.entries(data)
.filter(([key]) => key !== 'id' && key !== 'role')
.map(([key, value]) => {
return (
<div key={key.id}>
<p>
{value}
</p>
</div>
);
});
It is a good place to use Array.filter
, but if you know which fields you want to show and there are only three you can do it like this:这是使用
Array.filter
的好地方,但是如果您知道要显示哪些字段并且只有三个字段,您可以这样做:
const { email, firstName, lastName } = data;
return (
<div>
<p>
{email}
</p>
</div>
<div>
<p>
{firstName}
</p>
</div>
<div>
<p>
{lastName}
</p>
</div>
);
The key is not needed.不需要密钥。 Also I'm not sure if
key.id
is a valid parameter here, I think that we are receiving a string, not an object from the Object.entries
array.另外我不确定
key.id
是否是一个有效参数,我认为我们正在接收一个字符串,而不是来自Object.entries
数组的 object。
The approach I showed is more descriptive, does not use a loop and it is easier to add <a>
for an email or put full name (first and last name) in one <h1>
HTML tag.我展示的方法更具描述性,不使用循环,并且更容易为 email 添加
<a>
或将全名(名字和姓氏)放在一个<h1>
HTML 标记中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.