1 回答
TA贡献1804条经验 获得超7个赞
您可以为您的联系人按字母创建一个组,然后迭代这些字母,并为每个字母迭代联系人
export default class ShowContactsList extends Component {
render() {
const groupedByLetter = mockData.reduce((groups, contact) => {
const letter = contact.fname[0].toUpperCase();
const group = groups[letter] || [];
group.push(contact);
groups[letter] = group;
return groups;
}, {});
return Object.entries(groupedByLetter).map(([letter, contacts]) => {
return (
<div style={{ backgroundColor: "yellow", width: "80%" }}>
<h1>{letter}</h1>
{contacts.map((inputMap, index) => (
<ContactListOneContact
key={index}
lname={inputMap.lname}
fname={inputMap.fname}
contact={inputMap.contact}
/>
))}
</div>
);
});
}
}
添加回答
举报