[英]key value mapping with array object
I am stuck at mapping value with object.我被 object 困在映射值上。
I have Array of objects我有对象数组
const data = [
{
_id: "535345345345345345311",
name: "Activity",
description: "this is activity",
filters: []
},
{
_id: "535345345345345345311",
name: "Activity",
description: "this is activity",
filters: [
{ _id: "49823476237423984", name: "ACTIVITY filter", origin: "ACTIVITY" }
]
}
];
Here, like that array of object have user_of then it should show User Reading data from the object.在这里,就像 object 的数组一样,它具有 user_of,那么它应该显示来自 object 的用户读取数据。 Or if data is not present at the object then show as it is.
或者,如果 object 中不存在数据,则按原样显示。 Like if there's data example_of its there in that above array object, but not in object then show example_of only....
就像上面的数组 object 中有数据 example_of 一样,但 object 中没有,则仅显示 example_of....
{
"user_of": "User",
"student_of": "Student",
"test_of": "TEST"
}
Where this is the object, now if data
has user_of
I want to show as User
in the result output.这是 object,现在如果
data
有user_of
,我想在结果 output 中显示为User
。 Similarly if I have student_of
in data
then I want to show Student
同样,如果我在
data
中有student_of
,那么我想显示Student
Below is the code where I am trying to achieve this.下面是我试图实现这一目标的代码。
{data.filters.map((filter, index) => (
<>
{index < 2 && (
<span style={{ textTransform: "capitalize" }}>
{filter.origin.toLowerCase()}
{index === 0 && record.filters.length > 1 && <span>, </span>}
</span>
)}
</>
))}
{data.filters.length > 2 && (
<span> +{record.filters.length - 2} </span>
)}
also Here's working example of the code https://codesandbox.io/s/hardcore-kilby-hhbon?file=/index.js这也是代码https://codesandbox.io/s/hardcore-kilby-hhbon?file=/index.js的工作示例
What I am getting currently is below我目前得到的如下
And here's what I expect is这就是我所期望的
Basically all I want is replace the value with that object value(for naming convention purpose).基本上我想要的只是用 object 值替换该值(用于命名约定)。 You can see this on codesandbox link above, I am confused.
你可以在上面的代码框链接上看到这个,我很困惑。 (New to JS world, Previously with python)
(JS世界的新手,以前用python)
Can someone help me?有人能帮我吗?
You're pretty close:你非常接近:
...
<>
{index < 2 && (
<span style={{ textTransform: "capitalize" }}>
{objet[filter.origin]}
{index === 0 && record.filters.length > 1 && <span>, </span>}
</span>
)}
</>
...
I just changed this to return {objet[filter.origin]}
我只是将其更改为返回
{objet[filter.origin]}
objet
is already defined but you need to add activity_of
objet
已定义,但您需要添加activity_of
const objet = {
user_of: "User",
student_of: "Student",
test_of: "TEST",
activity_of: "Activity"
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.