如何在地图函数内累积数组?

时间:2016-10-07 16:20:47

标签: javascript node.js sequence reduce

我完全混淆了如何实现累积数组。我认为在reduce方法中这样做的最好方法。但是我很少与它混淆 目前我的输出看起来像这样.. 提前谢谢

输出

    Jobs Inserted ====================>
    JobId:j8 Time to finish this Job :32
    JobId:j7 Time to finish this Job :22
    JobId:j2 Time to finish this Job :9
    JobId:j5 Time to finish this Job :8
    JobId:j4 Time to finish this Job :7
    JobId:j6 Time to finish this Job :5
    JobId:j1 Time to finish this Job :4
    JobId:j3 Time to finish this Job :1
    Machines Available :4
    Output =====================================>
//I want the result should be cummulative array
    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
  

例:-j5 = 8,J4 = 7 + 8 = 15,J 3 = 16

像这样

 MachineId:M1 Jobs done By this Machine:(j5 = 8),(j4 = 15),(j3 = 16) Time consumed to finish all jobs:16
        MachineId:M2 Jobs done By this Machine:(j2 = 9),(j6 = 14),(j1 = 18) 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

如何实现这个...... 这是我的源代码

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

1 个答案:

答案 0 :(得分:0)

好的,所以我发现了你的问题。在这种情况下,您需要逐个构建字符串,附加每个作业编号和当前运行的总时间。您可以使用前面提到的.reduce()函数执行此操作,也可以创建一个保持总计的变量,并使用基本的forEach()reduce()会更清晰,但如果您将来重构代码,forEach()可能会更容易理解 - 老实说,这是您的选择。以下是我使用reduce()

的方式

如果您有任何疑问,请与我联系!

machines.map(function (data) {
  var str = "MachineId:" + data.name + " Jobs done by this Machine:"
  data.jobs.reduce(function (total, curr){
    var newTotal = total + curr.time;
    str += "("+curr.jobname+" = "+newTotal+")";
    return newTotal;
  }, 0);
  str += ' Time consumed to finish all jobs:'+data.value;
  console.log(str);
});

完全正常工作的DEMO

&#13;
&#13;
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) {
  var str = "MachineId:" + data.name + " Jobs done by this Machine:"
  data.jobs.reduce(function(total, curr) {
    var newTotal = total + curr.time;
    str += "(" + curr.jobname + " = " + newTotal + ")";
    return newTotal;
  }, 0);
  str += ' Time consumed to finish all jobs:' + data.value;
  console.log(str);
});



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);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
&#13;
&#13;
&#13;