按首字母索引存储联系人列表
我正在制作按字母顺序排列的联系人列表,我当前存储的方式是在这样的对象内:
{
"&": [
{
"key": "20444358",
"initials": "@#",
"name": "@name #"
},
{
"key": "20444669",
"initials": "@@",
"name": "@ @"
}
],
"#": [
{
"key": "4419686",
"initials": "1",
"name": "1075621114"
},
{
"key": "41604990",
"initials": "12",
"name": "123 Test 23"
},
{
"key": "32783347",
"initials": "2",
"name": "232323"
}
],
"A": [
{
"key": "20444317",
"initials": "AJ",
"name": "Àbdon Jua"
},
{
"key": "20444454",
"initials": "AA",
"name": "Abraão Moura"
}
]
}
我目前面临两个问题:
- 当我获取新的联系人页面并需要将它们与当前对象我正在这样做:
contacts = {...contacts, ...newContacts}
- 我将它们渲染在 FlatList 上,并且很难获取 Index。
这是我的获取:
if (response.data !== []) {
for (let i = 0; i < response.data.length; i++) {
let indexInitial = getIndexInitial(response.data[i].name)
if (!(indexInitial in newContacts)) {
newContacts[indexInitial] = [{
key: response.data[i].id.toString(),
initials: getInitials(response.data[i].name),
name: response.data[i].name
}]
} else {
newContacts[indexInitial].push({
key: response.data[i].id.toString(),
initials: getInitials(response.data[i].name),
name: response.data[i].name
})
}
}
pushContacts(newContacts);
page.current = currentPage + 1;
} else {
setEndOfTheList(true)
}
I'm making a contact list ordered alphabetically, the way that I'm currently storing is inside an object like this:
{
"&": [
{
"key": "20444358",
"initials": "@#",
"name": "@name #"
},
{
"key": "20444669",
"initials": "@@",
"name": "@ @"
}
],
"#": [
{
"key": "4419686",
"initials": "1",
"name": "1075621114"
},
{
"key": "41604990",
"initials": "12",
"name": "123 Test 23"
},
{
"key": "32783347",
"initials": "2",
"name": "232323"
}
],
"A": [
{
"key": "20444317",
"initials": "AJ",
"name": "Àbdon Jua"
},
{
"key": "20444454",
"initials": "AA",
"name": "Abraão Moura"
}
]
}
I'm facing two problems at the moment:
- When I fetch a new page of contacts and need to merge them with the current object I'm doing like this:
contacts = {...contacts, ...newContacts}
- I'm rendering them on a FlatList, and having a hard time getting the Index.
This is my fetch:
if (response.data !== []) {
for (let i = 0; i < response.data.length; i++) {
let indexInitial = getIndexInitial(response.data[i].name)
if (!(indexInitial in newContacts)) {
newContacts[indexInitial] = [{
key: response.data[i].id.toString(),
initials: getInitials(response.data[i].name),
name: response.data[i].name
}]
} else {
newContacts[indexInitial].push({
key: response.data[i].id.toString(),
initials: getInitials(response.data[i].name),
name: response.data[i].name
})
}
}
pushContacts(newContacts);
page.current = currentPage + 1;
} else {
setEndOfTheList(true)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论