2015-06-14 27 views
0

我有相當複雜的數組,我需要轉換爲特定的格式。陣列看起來像這樣:從數組中選擇最小數並刪除其餘的數,javascript

arr = [["name1",51,1,"code1",3],["name2",52,0,"code2",4,"code3",6],["name3",51,2,"code4",3,"code5",6,"code6",1],["name4",55,5,"code7",7,"code8",1],["name5",54,2,"code9",5,"code10",8]]; 

我輸出中的每個數組都需要只包含5個值 - 3個第一個值始終與輸入中的值相同。接下來的2個值應該包含代碼,並從數組的其餘部分包含最小值。所以,這種情況下輸出看起來就像這樣:

output = [["name1",51,1,"code1",3],["name2",52,0,"code2",4],["name3",51,2,"code6",1],["name4",55,5,"code8",1],["name5",54,2,"code9",5]]; 

對於開始我覺得最好的是,如果使用指令循環,但我不知道怎麼後來就應對這一點。

for (i=0; i<arr.length; i++) { 
    if (arr[i]>5) { 
    //dont know what to put here 
    } 
} 

回答

0

我的解決辦法:

arr = [ 
 
    ["name1",51,1,"code1",3], 
 
    ["name2",52,0,"code2",4,"code3",6], 
 
    ["name3",51,2,"code4",3,"code5",6,"code6",1], 
 
    ["name4",55,5,"code7",7,"code8",1], 
 
    ["name5",54,2,"code9",5,"code10",8] 
 
]; 
 

 
function process_array(in_array) { 
 
    
 
    var output = []; 
 
    
 
    /* check each element in array */ 
 
    in_array.forEach(function(sub_array){ 
 
     
 
     /* store new sub array here*/ 
 
     var last_sub_out = []; 
 
     
 
     var code; 
 
     var lowest_num = Number.MAX_VALUE; 
 
     
 
     /* check sub array 
 
      @value is value of sub_array 
 
      @index is index of that value. 
 
     */ 
 
     sub_array.forEach(function(value, index){ 
 
      
 
      /* add first 3 values */ 
 
      if(index < 3) { 
 
       last_sub_out.push(value); 
 
       return; 
 
      } 
 
      
 
      /* checking only numbers(even index: 4,6,8, ...) */ 
 
      if(index % 2 == 0) { 
 
       if(value < lowest_num) { 
 
        code = sub_array[index - 1]; 
 
        lowest_num = value; 
 
       } 
 
      } 
 
     }); 
 
     
 
     /* add found code with lowest number */ 
 
     last_sub_out.push(code, lowest_num); 
 
     
 
     output.push(last_sub_out); 
 
     
 
     /* LOG */ 
 
     document.write(last_sub_out.join(', ') + "</br>"); 
 
    }); 
 
    return output; 
 
} 
 

 
var out = process_array(arr);

0

嘗試:

var arr = [["name1",51,1,"code1",3],["name2",52,0,"code2",4,"code3",6],["name3",51,2,"code4",3,"code5",6,"code6",1],["name4",55,5,"code7",7,"code8",1],["name5",54,2,"code9",5,"code10",8]]; 
 
var output = [], startIndex = 3, i = 0; 
 

 
while(i < arr.length){ 
 
    var item = arr[i++], j = startIndex, count = 0, min = Infinity, code; 
 
    while(j < item.length){ 
 
    count++ % 2 && item[j] < min && (min = item[j], code = item[j-1]); 
 
    j++ 
 
    } 
 
    item.splice(startIndex, item.length, code, min); 
 
    output.push(item) 
 
} 
 

 

 
document.write("<pre>" + JSON.stringify(output, null, 4) + "<pre>");

0
var arr = [["name1",51,1,"code1",3],["name2",52,0,"code2",4,"code3",6],["name3",51,2,"code4",3,"code5",6,"code6",1],["name4",55,5,"code7",7,"code8",1],["name5",54,2,"code9",5,"code10",8]]; 
var out=[]; 
for (i=0; i < arr.length; i++) { 
    if (arr[i].length>5) { 
     out[i] = [arr[i][0], arr[i][1], arr[i][2]]; 
     c = [arr[i][3], arr[i][4]]; 
     for (j=0; j < (arr[i].length - 5)/2; j++){ 
      if (c[1] > arr[i][6+2*j]){ 
      c = [arr[i][5+2*j], arr[i][6+2*j]]; 
      } 
     } 
     out[i][3] = c[0]; out[i][4] = c[1]; 
    } else { 
     out[i] = arr[i] 
    } 
} 
相關問題