如何在 map 函数中累加数组?

How to cummulative sum of array inside map function?

我完全不知道如何实现累积 array.I 认为最好的方法是减少 method.But 我对此有点困惑 目前我的输出看起来像这样.. 提前致谢

输出

    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

Ex:-j5=8,j4=7+8=15,j3=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);

好的,我发现了你的问题。在这种情况下,您需要一段一段地构建字符串,并附加每个作业编号和当前的 运行 总时间。您可以使用前面提到的 .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);
});

完全可用的演示

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