admin管理员组文章数量:1356844
Here is my Parent ponent's render function:
render() {
const users = [
'tom': {
phone: '123',
email: 'hotmail'
},
'rob': {
phone: '321',
email: 'yahoo'
},
'bob': {
phone: '333',
email: 'gmail'
},
];
const list = users.map((user) =>
(<User
name={user}
phone={users.phone}
email={users.email}
/>),
);
return <ul>{list}</ul>;
}
The output shows up like this:
And here is the Child ponent's render function:
render() {
const {
name,
phone,
email,
} = this.props;
const info = [name, phone, email];
const item = info.map((index) => (
<li key={index}>
{ index }
</li>
));
return item;
}
How can I get it to show with the phone numbers and emails? Not sure what I'm doing incorrectly. Thanks.
Here is my Parent ponent's render function:
render() {
const users = [
'tom': {
phone: '123',
email: 'hotmail'
},
'rob': {
phone: '321',
email: 'yahoo'
},
'bob': {
phone: '333',
email: 'gmail'
},
];
const list = users.map((user) =>
(<User
name={user}
phone={users.phone}
email={users.email}
/>),
);
return <ul>{list}</ul>;
}
The output shows up like this:
And here is the Child ponent's render function:
render() {
const {
name,
phone,
email,
} = this.props;
const info = [name, phone, email];
const item = info.map((index) => (
<li key={index}>
{ index }
</li>
));
return item;
}
How can I get it to show with the phone numbers and emails? Not sure what I'm doing incorrectly. Thanks.
Share Improve this question asked May 8, 2018 at 18:58 Ralph David AbernathyRalph David Abernathy 5,51813 gold badges56 silver badges87 bronze badges 1-
Your
users
variable is assigned an array that looks more like an object... – Code-Apprentice Commented May 8, 2018 at 19:03
5 Answers
Reset to default 5At first this is not valid javascript:
const users = [
'tom': {
phone: '123',
email: 'hotmail'
},
// ...
];
You should either define an array or an object. Let's say your users
is an object literal:
const users = {
'tom': {
phone: '123',
email: 'hotmail'
},
// ...
};
Then you can iterate over entries of the object:
const list = Object.entries(users).map(([name, info]) =>
(<User
name={name}
phone={info.phone}
email={info.email}
/>)
);
However Object.entries()
is not supported in all browsers, check if it works in your environment.
First thing is that users
is not a valid array If you want key value pair in main scope then use Object
({})
render() {
const users = {
'tom': {
phone: '123',
email: 'hotmail'
},
'rob': {
phone: '321',
email: 'yahoo'
},
'bob': {
phone: '333',
email: 'gmail'
},
};
const list = Object.keys(users).map((user) =>
(<User
name={user}
phone={users[user].phone}
email={users[user].email}
/>),
);
return <ul>{list}</ul>;
}
You need to be referring to "user" instead of "users" inside of the Parent ponent's map function. The purpose of the "user" variable is to represent the current instance of each element in the array. So the map function should look like:
const list = users.map((user) =>
(<User
name={user}
phone={user.phone}
email={user.email}
/>),
);
instead.
One solution is to change your "list" to an actual list:
const users = [
{
name: 'tom',
phone: '123',
email: 'hotmail'
},
{
name: 'rob,
phone: '321',
email: 'yahoo'
},
{
name: 'bob',
phone: '333',
email: 'gmail'
},
];
Now user user.name
instead of user
.
Alternatively, create an object keyed by the names of each user:
const users = {
'tom': {
phone: '123',
email: 'hotmail'
},
'rob': {
phone: '321',
email: 'yahoo'
},
'bob': {
phone: '333',
email: 'gmail'
},
};
Then map over the keys:
const list = Object.keys(users).map((user) =>
(<User
name={user}
phone={users[user].phone}
email={users[user].email}
/>),
);
Your const users should be modified to map through them.
const users = [
{
name: 'tom',
phone: '123',
email: 'hotmail',
},
{
name:'rob',
phone: '321',
email: 'yahoo'
},
{
name: 'bob',
phone: '333',
email: 'gmail'
},
];
const list = users.map((usr =>
(<User
name={usr.name}
phone={usr.phone}
email={usr.email}
/>),
);
return <ul>{list}</ul>;
}
本文标签:
版权声明:本文标题:javascript - How to loop through properties of objects inside of an array within a React component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744031205a2578940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论