关闭

js实现城市名称拼音索引

标签: 函数jsonjavascript
1481人阅读 评论(0) 收藏 举报
分类:

来到新的项目组快两个月的时间了,今天开需求分析电话会议时;业务提出来根据拼音进行城市姓名索引的需求。所以自己编写了一个方法进行从后台得到的json数据进行一个按照城市首写字母的城市索引!代码如下:

$rootScope.getLastCityList = function(list){
      var scope = this;
      if(list){
         var TheTempCityList = new Array();
         for(var i=0;i<list.length;i++){
            for(var j=0;j<list[i].cityModelList.length;j++){
               TheTempCityList.push(list[i].cityModelList[j]);
            }
         }
         //过滤出所有的城市列表
         var AObjectList=[];
         var BObjectList=[];
         var CObjectList=[];
         var DObjectList=[];
         var EObjectList=[];
         var FObjectList=[];
         var GObjectList=[];
         var HObjectList=[];
         var IObjectList=[];
         var JObjectList=[];
         var KObjectList=[];
         var LObjectList=[];
         var MObjectList=[];
         var NObjectList=[];
         var OObjectList=[];
         var PObjectList=[];
         var QObjectList=[];
         var RObjectList=[];
         var SObjectList=[];
         var TObjectList=[];
         var UObjectList=[];
         var VObjectList=[];
         var WObjectList=[];
         var XObjectList=[];
         var YObjectList=[];
         var ZObjectList=[];
         var AObject = {
            "cityFlagName":"A",
            "cityFlagNameList":AObjectList
         };
         var BObject = {
            "cityFlagName":"B",
            "cityFlagNameList":BObjectList
         };
         var CObject = {
            "cityFlagName":"C",
            "cityFlagNameList":CObjectList
         };
         var DObject = {
            "cityFlagName":"D",
            "cityFlagNameList":DObjectList
         };
         var EObject = {
            "cityFlagName":"E",
            "cityFlagNameList":EObjectList
         };
         var FObject = {
            "cityFlagName":"F",
            "cityFlagNameList":FObjectList
         };
         var GObject = {
            "cityFlagName":"G",
            "cityFlagNameList":GObjectList
         };
         var HObject = {
            "cityFlagName":"H",
            "cityFlagNameList":HObjectList
         };
         var IObject = {
            "cityFlagName":"I",
            "cityFlagNameList":IObjectList
         };
         var JObject = {
            "cityFlagName":"J",
            "cityFlagNameList":JObjectList
         };
         var KObject = {
            "cityFlagName":"K",
            "cityFlagNameList":KObjectList
         };
         var LObject = {
            "cityFlagName":"L",
            "cityFlagNameList":LObjectList
         };
         var MObject = {
            "cityFlagName":"M",
            "cityFlagNameList":MObjectList
         };
         var NObject = {
            "cityFlagName":"N",
            "cityFlagNameList":NObjectList
         };
         var OObject = {
            "cityFlagName":"O",
            "cityFlagNameList":OObjectList
         };
         var PObject = {
            "cityFlagName":"P",
            "cityFlagNameList":PObjectList
         };
         var QObject = {
            "cityFlagName":"Q",
            "cityFlagNameList":QObjectList
         };
         var RObject = {
            "cityFlagName":"R",
            "cityFlagNameList":RObjectList
         };
         var SObject = {
            "cityFlagName":"S",
            "cityFlagNameList":SObjectList
         };
         var TObject = {
            "cityFlagName":"T",
            "cityFlagNameList":TObjectList
         };
         var UObject = {
            "cityFlagName":"U",
            "cityFlagNameList":UObjectList
         };
         var VObject = {
            "cityFlagName":"V",
            "cityFlagNameList":VObjectList
         };
         var WObject = {
            "cityFlagName":"W",
            "cityFlagNameList":WObjectList
         };
         var XObject = {
            "cityFlagName":"X",
            "cityFlagNameList":XObjectList
         };
         var YObject = {
            "cityFlagName":"Y",
            "cityFlagNameList":YObjectList
         };
         var ZObject = {
            "cityFlagName":"Z",
            "cityFlagNameList":ZObjectList
         };
         //将字母不同的城市,分别装进不同的对象
         for(var k=0;k<TheTempCityList.length;k++){
            switch(TheTempCityList[k].cityFlag){
               case 'a':
               case 'A':
                  AObjectList.push(TheTempCityList[k]);
                  break;
               case 'b':
               case 'B':
                  BObjectList.push(TheTempCityList[k]);
                  break;
               case 'c':
               case 'C':
                  CObjectList.push(TheTempCityList[k]);
                  break;
               case 'd':
               case 'D':
                  DObjectList.push(TheTempCityList[k]);
                  break;
               case 'e':
               case 'E':
                  EObjectList.push(TheTempCityList[k]);
                  break;
               case 'f':
               case 'F':
                  FObjectList.push(TheTempCityList[k]);
                  break;
               case 'g':
               case 'G':
                  GObjectList.push(TheTempCityList[k]);
                  break;
               case 'h':
               case 'H':
                  HObjectList.push(TheTempCityList[k]);
                  break;
               case 'i':
               case 'I':
                  IObjectList.push(TheTempCityList[k]);
                  break;
               case 'j':
               case 'J':
                  JObjectList.push(TheTempCityList[k]);
                  break;
               case 'k':
               case 'K':
                  KObjectList.push(TheTempCityList[k]);
                  break;
               case 'l':
               case 'L':
                  LObjectList.push(TheTempCityList[k]);
                  break;
               case 'm':
               case 'M':
                  MObjectList.push(TheTempCityList[k]);
                  break;
               case 'n':
               case 'N':
                  NObjectList.push(TheTempCityList[k]);
                  break;
               case 'o':
               case 'O':
                  OObjectList.push(TheTempCityList[k]);
                  break;
               case 'p':
               case 'P':
                  PObjectList.push(TheTempCityList[k]);
                  break;
               case 'q':
               case 'Q':
                  QObjectList.push(TheTempCityList[k]);
                  break;
               case 'r':
               case 'R':
                  RObjectList.push(TheTempCityList[k]);
                  break;
               case 's':
               case 'S':
                  SObjectList.push(TheTempCityList[k]);
                  break;
               case 't':
               case 'T':
                  TObjectList.push(TheTempCityList[k]);
                  break;
               case 'u':
               case 'U':
                  UObjectList.push(TheTempCityList[k]);
                  break;
               case 'v':
               case 'V':
                  VObjectList.push(TheTempCityList[k]);
                  break;
               case 'w':
               case 'W':
                  WObjectList.push(TheTempCityList[k]);
                  break;
               case 'x':
               case 'X':
                  XObjectList.push(TheTempCityList[k]);
                  break;
               case 'y':
               case 'Y':
                  YObjectList.push(TheTempCityList[k]);
                  break;
               case 'z':
               case 'Z':
                  ZObjectList.push(TheTempCityList[k]);
                  break;
            }
         }
         //将最终的城市对象,装进想要的城市列表
         var TheLastCityList = [AObject,BObject,CObject,DObject,EObject,FObject,GObject,
            HObject,IObject,JObject,KObject,LObject,MObject,NObject,
            OObject,PObject,QObject,RObject,SObject,TObject,
            UObject,VObject,WObject,XObject,YObject,ZObject];
         var FilterLastCityList = new Array();
         for(var i=0;i<TheLastCityList.length;i++){
            if(TheLastCityList[i].cityFlagNameList.length>0){
               FilterLastCityList.push(TheLastCityList[i]);
            }
         }
         scope.TheLastCityList = FilterLastCityList;
      }
   };
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:77433次
    • 积分:1091
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:2篇
    • 译文:2篇
    • 评论:6条
    文章分类
    最新评论