按首字母索引存储联系人列表

发布于 2025-01-11 09:13:04 字数 1597 浏览 4 评论 0原文

我正在制作按字母顺序排列的联系人列表,我当前存储的方式是在这样的对象内:

{
  "&": [
    {
      "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"
    }
  ]
}

我目前面临两个问题:

  1. 当我获取新的联系人页面并需要将它们与当前对象我正在这样做: contacts = {...contacts, ...newContacts}
  2. 我将它们渲染在 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:

  1. 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}
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文