Select minimum number from array and delete the rest, javascript -


i have quite complicated array need transform specific format. array looks this:

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

each array in output need contains 5 values - 3 first values same in input. next 2 values should contain code , lowest value rest of array. case output this:

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 start think best use loop , if instruction, don't know how cope later on.

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

my solution:

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 value of sub_array            @index index of value.         */        sub_array.foreach(function(value, index){                        /* add first 3 values */            if(index < 3) {                last_sub_out.push( value );                return;            }                        /* checking 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 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);


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -