从数组中选择最小数字并删除其余的javascript

时间:2015-06-14 13:35:35

标签: javascript arrays

我有一个非常复杂的数组,我需要转换为特定的格式。该数组如下所示:

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]];

首先,我认为最好的是使用循环和if指令,但我不知道如何在以后处理它。

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

尝试:

&#13;
&#13;
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>");
&#13;
&#13;
&#13;

答案 2 :(得分: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]
    }
}