pinyin3.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import vPinyin from './pinyin2.js';
  2. var Letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U','V', 'W', 'X', 'Y', 'Z', '#'];
  3. export default {
  4. paixu: function(ary) {
  5. var e = []
  6. e = ary
  7. /*按字母排序分开*/
  8. var list = []
  9. e.forEach((value,index)=>{
  10. var pinyin = vPinyin.chineseToPinYin(value.name)[0]
  11. Letters.forEach((value2, index2) => {
  12. if (value2 == pinyin) {
  13. var contact = {
  14. letter: value2,
  15. value: {
  16. name: value.name,
  17. phone: value.phone.replace(/\s*/g,""),
  18. }
  19. }
  20. list = list.concat(contact)
  21. }
  22. })
  23. })
  24. /*获取所有字母字母去掉*/
  25. var list2 = []
  26. list.forEach((value, index) => {
  27. var map = list[index].letter
  28. list2 = list2.concat(map)
  29. })
  30. /*去掉相同的字母*/
  31. var newArr = [];
  32. for (var i = 0; i < list2.length - 1; i++) {
  33. if (newArr.indexOf(list2[i]) == -1) {
  34. newArr.push(list2[i]);
  35. }
  36. }
  37. /*给数组letter赋值字母*/
  38. var list3 = []
  39. newArr.forEach((value, index) => {
  40. var map = {
  41. letter: value,
  42. contacts: []
  43. }
  44. list3 = list3.concat(map)
  45. })
  46. /*得到数组*/
  47. list3.forEach((value, index) => {
  48. list.forEach((value2, index2) => {
  49. if (value.letter == value2.letter) {
  50. var map = value2.value
  51. value.contacts = value.contacts.concat(map)
  52. }
  53. })
  54. })
  55. return list3;
  56. }
  57. }