2015-06-14 3 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

Try:

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] 
    } 
} 
Смежные вопросы