2016-10-13 1 views
1

Я новичок в javascript.I довольно запутанный, что, как преобразовать мой вывод MATIX образования, как это ..Как конвертировать мой вывод матрицы в Javascript

Из этого вывода на

Выход

MachineId:M1 Jobs done By this Machine:(j5 = 8),(j4 = 7),(j3 = 1) Time consumed to finish all jobs:16 
MachineId:M2 Jobs done By this Machine:(j2 = 9),(j6 = 5),(j1 = 4) Time consumed to finish all jobs:18 
MachineId:M3 Jobs done By this Machine:(j7 = 22) Time consumed to finish all jobs:22 
MachineId:M4 Jobs done By this Machine:(j8 = 32) Time consumed to finish all jobs:32 

Этот вывод должен быть напечатан

[8,7,1][16], 
    [9,5,4][18], 
    [22,0,0][22], 
    [32,0,0][32] 

Это мой фактический код р родить первый упомянутый вывод

// real algorithm 
    //total Jobs 
    var _ = require('underscore'); 
    var njobs = [{ 
     jobname: "j1", 
     time: 4 
    }, { 
     jobname: "j2", 
     time: 9 
    }, { 
     jobname: "j3", 
     time: 1 
    }, { 
     jobname: "j4", 
     time: 7 
    }, { 
     jobname: "j5", 
     time: 8 
    }, { 
     jobname: "j6", 
     time: 5 
    }, { 
     jobname: "j7", 
     time: 22 
    }, 
     { 
      jobname: "j8", 
      time: 32 
     } 

    ]; 

    //sort this data with time 
    var sorted = njobs.sort((a, b) => b.time - a.time); 

    console.log('Jobs Inserted ====================>') 

    sorted.map(function (data) { 
     console.log('JobId:'+data.jobname+' Time to finish this Job :'+data.time) 
    }); 

    //List all the machines 
    var machines = [ 
     { 
      id:1, 
      value:0, 
      jobs:[], 
      name:'M1' 
     }, 
     { 
      id:2, 
      value:0, 
      jobs:[], 
      name:'M2' 
     }, 
     { 
      id:3, 
      value:0, 
      jobs:[], 
      name:'M3' 
     }, 
     { 
      id:3, 
      value:0, 
      jobs:[], 
      name:'M4' 
     } 
    ]; 
    console.log('Machines Available :'+ machines.length); 
    //Loop it and assign it 
    sorted.forEach(job => { 
     var minMachine = machines 
      .slice(1) 
      .reduce((res, cur) => 
       res.value < cur.value ? res : cur, machines[0]); 

     minMachine.jobs.push(job); 
     minMachine.value += job.time; 
    }); 


    //Prints the value 

    console.log('Output =====================================>'); 



    machines.map(function (data) { 
     console.log('MachineId:'+data.name+' Jobs done By this Machine:'+data.jobs.map(data => '('+data.jobname+' = '+data.time+')')+' Time consumed to finish all jobs:'+data.value) 
    }); 



    var high = Math.max.apply(Math,machines.map(function (o) { 
     return o.value; 
    })); 
    console.log('______________________________________________') 
    console.log('Highest Time consuming Machine =========>'); 
    var result = _.findWhere(machines,{value:high}); 
    console.log(result.name+' is the highest running Machine '+result.value); 

    // machines.map(function(){...}).reduce(function (total, curr) { return total + curr; }); 

ответ

0

Добавьте это в нижней части JavaScript.

// Build the multidimensional array. 
var arr = []; 
var maxJobsPerMachine = 0; 
machines.map(function (data) { 
    var marr = []; 
    marr.push(data.jobs.map(data => data.time)); 
    maxJobsPerMachine=Math.max(maxJobsPerMachine, marr[0].length); 
    marr.push([data.value]) 
    arr.push(marr); 
}); 
arr.forEach(e => { while (e[0].length < maxJobsPerMachine) e[0].push(0); }); 

// Print it. 
arr.forEach(e => { 
    var line = ""; 
    e.forEach(f => line += "[" + f.join(",") + "]"); 
    console.log(line); 
}); 

Вот результат:

[8,7,1][16] 
[9,5,4][18] 
[22,0,0][22] 
[32,0,0][32] 
+0

Thanx Bro..You велики – wrarar