var aVoteList1 = [ 
   {to_id : 1, s_id : 1, s_name : '湘湖站', type : 1, vote_num : 5}, 
   {to_id : 2, s_id : 1, s_name : '湘湖站', type : 2, vote_num : 10}, 
   {to_id : 3, s_id : 1, s_name : '湘湖站', type : 3, vote_num : 3}, 
   {to_id : 4, s_id : 2, s_name : '滨康路站', type : 1, vote_num : 0}, 
   {to_id : 5, s_id : 2, s_name : '滨康路站', type : 2, vote_num : 0}, 
   {to_id : 6, s_id : 2, s_name : '滨康路站', type : 3, vote_num : 0}, 
   null 
];var aVoteList2 = [ 
    [
     {to_id : 1, s_id : 1, s_name : '湘湖站', type : 1, vote_num : 5}, 
     {to_id : 2, s_id : 1, s_name : '湘湖站', type : 2, vote_num : 10}, 
     {to_id : 3, s_id : 1, s_name : '湘湖站', type : 3, vote_num : 3}
    ],
    [
     {to_id : 4, s_id : 2, s_name : '滨康路站', type : 1, vote_num : 0}, 
     {to_id : 5, s_id : 2, s_name : '滨康路站', type : 2, vote_num : 0}, 
     {to_id : 6, s_id : 2, s_name : '滨康路站', type : 3, vote_num : 0}
    ]
];上面的1和2分别如何转化为下面的3 的格式?var oVoteList3 = { 
  s_1 : [
  {to_id : 1, s_id : 1, s_name : '湘湖站', type : 1, vote_num : 5}, 
  {to_id : 2, s_id : 1, s_name : '湘湖站', type : 2, vote_num : 10}, 
  {to_id : 3, s_id : 1, s_name : '湘湖站', type : 3, vote_num : 3}
  ],
  s_2 : [
  {to_id : 4, s_id : 2, s_name : '滨康路站', type : 1, vote_num : 0}, 
  {to_id : 5, s_id : 2, s_name : '滨康路站', type : 2, vote_num : 0}, 
  {to_id : 6, s_id : 2, s_name : '滨康路站', type : 3, vote_num : 0}
  ]
}; 

解决方案 »

  1.   


     //第一个
                var aVoteList3 = {
                    s_1: [aVoteList1[0], aVoteList1[1], aVoteList1[2]],
                    s_2: [aVoteList1[3], aVoteList1[4], aVoteList1[5]]
                };
                //第二个
                var aVoteList3 = {
                    s_1: [aVoteList2[0][0], aVoteList2[0][1], aVoteList2[0][2]],
                    s_2: [aVoteList2[1][0], aVoteList2[1][1], aVoteList2[1][2]]
               };
      

  2.   

    ..这是手动的, 要用语句for循环之类的 算法写呀
      

  3.   


    ..这是手动的, 要用语句for循环之类的 算法写呀 
      

  4.   

    function inArray(value,arr){
    if(!arr || arr.length == 0)
    return -1;
    for(var i = 0, len = arr.length; i < len; i++){
    if(value == arr[i]){
    return i;
    }
    }
    return -1;
    }
    function bianli(arr,sids,oVoteList3){
    for(var i = 0, len = arr.length; i < len; i++){
    var item = arr[i];
    if(item == null)
    continue;
    if(item instanceof Array){
    bianli(item,sids,oVoteList3);
    }else{
    var s_id = item.s_id;
    if(inArray(s_id,sids) == -1){
    sids.push(s_id);
    oVoteList3["s_"+s_id+""]= new Array();
    oVoteList3["s_"+s_id+""].push(item);
    }else{
    oVoteList3["s_"+s_id+""].push(item);
    }
    }
    }
    }
    function chongzu(arr){
    var oVoteList3 = {};
    var sids = new Array();
    bianli(arr,sids,oVoteList3);
    return oVoteList3;
    }
    var target1 = chongzu(aVoteList1);
    var target2 = chongzu(aVoteList2);