美文网首页
2018-12-16demo

2018-12-16demo

作者: 城东那个小丑 | 来源:发表于2018-12-16 20:43 被阅读0次
     //  例:有两个数组如下, 要求实现groupBy函数:
    
           let  targetList  = [
                 {
                     'name':'中国',
                     "address":"北京",
                     "area":"东城区"
    
                 },  {
                     'name':'中国',
                     "address":"陕西",
                     "area":"长安区"
    
                 },  {
                     'name':'美国',
                     "address":"伦登",
                     "area":"inter Faded"
    
                 },  {
                     'name':'美国',
                     "address":"纽约",
                     "area":"June City"
    
                 }
             ]
    
         fieldList = ['name']
    
       var rs =   groupBy(targetList,fieldList);//targetList要分组的数组, fieldList分组的字段数组
         //rs 结果如下
         rs = [
             {
                 "key":"中国",
                 "value":[
                     {
                         'name':'中国',
                         "address":"北京",
                         "area":"东城区"
    
                     },  {
                         'name':'中国',
                         "address":"陕西",
                         "area":"长安区"
    
                     }
                 ]
             },
             {
                 "key":"美国",
                 "value":[
                     {
                         'name':'美国',
                         "address":"伦登",
                         "area":"inter Faded"
    
                     },  {
                         'name':'美国',
                         "address":"纽约",
                         "area":"June City"
    
                     }
                 ]
             }
    
    
         ]
    
    
    
        //例2:
         fieldList = ['name','address']
    
         var rs =   groupBy(targetList,fieldList);//targetList要分组的数组, fieldList分组的字段数组
            //rs 输出如下
         rs = [
             {
                 "key":"中国",
                 "value":[
                     {
                         "key":"北京",
                         "value":[
                             {
                                 'name':'中国',
                                 "address":"北京",
                                 "area":"东城区"
    
                             }
                         ]
                     },{
                         "key":"陝西",
                         "value":[
                             {
                                 'name':'中国',
                                 "address":"陕西",
                                 "area":"长安区"
    
                             }
                         ]
                     }
                 ]
             },
             {
                 "key":"美国",
                 "value": [
                     {
                         "key":"伦登",
                         "value":[
                             {
                                 'name':'美国',
                                 "address":"伦登",
                                 "area":"inter Faded"
    
                             }
                         ]
                     },{
                         "key":"紐約",
                         "value":[
                             {
                                 'name':'美国',
                                 "address":"纽约",
                                 "area":"June City"
    
                             }
                         ]
                     }
                 ]
             }
    
    
         ]
    

    相关文章

      网友评论

          本文标题:2018-12-16demo

          本文链接:https://www.haomeiwen.com/subject/sgoxkqtx.html