如何避免以下代码中的for循环

时间:2017-03-24 22:29:46

标签: javascript nested-loops

Index.js

std::conditional

我正在尝试使用map或reduce但是无法真正弄清楚如何执行此操作。 如何有效地避免使用for循环,可以使用复合函数或回调函数来实现相同的功能?任何帮助将不胜感激!我

3 个答案:

答案 0 :(得分:1)

如果你制造的物体在每架飞机轮胎更换前有最大着陆量,例如

var maxLandings = {
    FooPlane: 10,
    BarPlane: 40
};

然后,您可以对数组应用过滤器以返回所需的数据:

const aircraftData = [{
      id: 1,
      lastTireChange: new Date('9/1/2013'),
      manufacturer: 'FooPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
      ]
    }, {
      id: 2,
      lastTireChange: new Date('10/29/2013'),
      manufacturer: 'FooPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
        new Date('1/1/2016'),
        new Date('10/1/2015'),
        new Date('10/23/2015')
      ]
    }, {
      id: 2,
      lastTireChange: new Date('10/29/2013'),
      manufacturer: 'FooPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
        new Date('1/1/2016'),
        new Date('10/1/2015'),
        new Date('10/23/2015')
      ]
    }, {
      id: 3,
      lastTireChange: new Date('9/1/2013'),
      manufacturer: 'BarPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
        new Date('1/1/2016'),
        new Date('10/1/2015'),
        new Date('10/23/2015'),
        new Date('9/12/2016'),
        new Date('10/28/2015'),
        new Date('11/15/2015'),
        new Date('12/22/2015'),
        new Date('12/22/2015')
      ]
    }];
    
const maxLandings = {
    FooPlane: 25,
    BarPlane: 3
};

    function getAircraftsDueForTireChange(allAircraft, maxLandings) {
        return allAircraft.filter(aircraft => {
            var landingsSinceLastTireChange = aircraft.landings.filter(landing => {
                return landing.getTime() >= aircraft.lastTireChange.getTime();
            });
            return landingsSinceLastTireChange.length >= maxLandings[aircraft.manufacturer];
        });
    }
    
  console.log(getAircraftsDueForTireChange(aircraftData, maxLandings))

答案 1 :(得分:1)

您的输入数据无效JavaScript,因此我将其修改为有效用于测试目的。

首先创建一个对象来存储每个OEM的轮胎更换间隔,这允许我们删除多个if语句,而不是单个语句。

接下来,在飞机阵列上使用Array#filter。在回调中,在给定飞机的着陆阵列上运行Array#filter,如果自上次轮胎更换后发生着陆,则返回true,然后将得到的阵列长度与相应的OEM轮胎更换间隔进行比较飞机。



window.CAMP = { aircraftData: [{ id: 1, lastTireChange: new Date('9/1/2013'), manufacturer: 'FooPlane', landings: [ new Date('9/1/2015'), new Date('9/5/2015'), new Date('9/12/2016'), new Date('9/12/2015') ] }, { id: 2, lastTireChange: new Date('10/29/2013'), manufacturer: 'FooPlane', landings: [ new Date('9/1/2015'), new Date('9/5/2015'), new Date('9/12/2016'), new Date('9/12/2015'), new Date('1/1/2016'), new Date('10/1/2015'), new Date('10/23/2015') ] }, { id: 2, lastTireChange: new Date('10/29/2013'), manufacturer: 'FooPlane', landings: [ new Date('9/1/2015'), new Date('9/5/2015'), new Date('9/12/2016'), new Date('9/12/2015'), new Date('1/1/2016'), new Date('10/1/2015'), new Date('10/23/2015') ] }, { id: 3, lastTireChange: new Date('9/1/2013'), manufacturer: 'BarPlane', landings: [ new Date('9/1/2015'), new Date('9/5/2015'), new Date('9/12/2016'), new Date('9/12/2015'), new Date('1/1/2016'), new Date('10/1/2015'), new Date('10/23/2015'), new Date('9/12/2016'), new Date('10/28/2015'), new Date('11/15/2015'), new Date('12/22/2015'), new Date('12/22/2015') ] }] };

function getAircraftsDueForTireChange(aircrafts) {
  // Changed FooPlane to 4 so we can show some passing the filter
  const intervals = { 'FooPlane': 4, 'BarPlane': 75, 'BazPlane': 200 };
  return aircrafts.filter(a => a.landings.filter(e => e.getTime() >= a.lastTireChange.getTime()).length >= intervals[a.manufacturer]);
}

console.log(getAircraftsDueForTireChange(window.CAMP.aircraftData).length); // 3




答案 2 :(得分:0)

我创建的代码与@ TinyGiant的答案完全相同。但我也做了一个有效的例子。我改变了着陆值和一些日期,以使代码适用于这个小样本。



window.CAMP = {
    aircraftData: [{
      // Should require maintenance
      id: 1,
      lastTireChange: new Date('9/1/2013'),
      manufacturer: 'FooPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
      ]
    }, {
      id: 2,
      lastTireChange: new Date('10/29/2013'),
      manufacturer: 'FooPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
        new Date('1/1/2016'),
        new Date('10/1/2015'),
        new Date('10/23/2015')
      ]
    }, {
      id: 2,
      lastTireChange: new Date('10/29/2013'),
      manufacturer: 'FooPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
        new Date('1/1/2016'),
        new Date('10/1/2015'),
        new Date('10/23/2015')
      ]
    }, {
      id: 3,
      lastTireChange: new Date('9/1/2013'),
      manufacturer: 'BarPlane',
      landings: [
        new Date('9/1/2015'),
        new Date('9/5/2015'),
        new Date('9/12/2016'),
        new Date('9/12/2015'),
        new Date('1/1/2016'),
        new Date('10/1/2015'),
        new Date('10/23/2015'),
        new Date('9/12/2016'),
        new Date('10/28/2015'),
        new Date('11/15/2015'),
        new Date('12/22/2015'),
        new Date('12/22/2015')
      ]
    }]
};

function getAircraftsDueForTireChange(allAircraftData) {
    var aircraftDueForTireChanges = [];
    for (var i = 0; i < allAircraftData.length; i++) {
        var landingsSinceLastTireChange = [];
        for (var j = 0; j < allAircraftData[i].landings.length; j++) {
            if (allAircraftData[i].landings[j] >= allAircraftData[i].lastTireChange)
                landingsSinceLastTireChange.push(allAircraftData[i].landings[j]);
        }
        if (allAircraftData[i].manufacturer == 'FooPlane' && landingsSinceLastTireChange.length >= 3)  // 120
            aircraftDueForTireChanges.push(allAircraftData[i]);
        else if (allAircraftData[i].manufacturer == 'BarPlane' && landingsSinceLastTireChange.length >= 2)  // 75
            aircraftDueForTireChanges.push(allAircraftData[i]);
        else if (allAircraftData[i].manufacturer == 'BazPlane' && landingsSinceLastTireChange.length >= 5) // 200
            aircraftDueForTireChanges.push(allAircraftData[i]);    
    }
    return aircraftDueForTireChanges;
}

function getAircraftsDueForTireChangeV2(allAircraftData) {
  var tireChangeMatrix = {FooPlane: 3, BarPlane: 2, BazPlane: 5};
  return allAircraftData.filter(
     aircraft=>aircraft.landings.filter(
       landing=>landing >= aircraft.lastTireChange).length>=tireChangeMatrix[aircraft.manufacturer]
  );
}
console.log(getAircraftsDueForTireChange(CAMP.aircraftData));
console.log(getAircraftsDueForTireChangeV2(CAMP.aircraftData));
&#13;
&#13;
&#13;

JSFiddle:https://jsfiddle.net/q0tanxda/5/

但也想指出原始代码中有2个错误:

function getAircraftsDueForTireChange(allAircraftData) {
  var aircraftDueForTireChanges = [];
  for (var i = 0; i < allAircraftData.length; i++) {
    var landingsSinceLastTireChange = [];
    for (var j = 0; j < allAircraftData[i].landings.length; j++) {
      if (allAircraftData[i].landings[j] >= allAircraftData[i].lastTireChange)
        landingsSinceLastTireChange.push(allAircraftData[i].landings[j]); 

      // ERROR: you included the below tests in every interaction of landings, 
      // the right would be calculate all the landing prior to the tests
      if (allAircraftData[i].manufacturer == 'FooPlane' && landingsSinceLastTireChange.length >= 120)
        aircraftDueForTireChanges.push(allAircraftData[i]);
      else if (allAircraftData[i].manufacturer == 'BarPlane' && landingsSinceLastTireChange.length >= 75)
        aircraftDueForTireChanges.push(allAircraftData[i]);
      else if (allAircraftData[i].manufacturer == 'BazPlane' && landingsSinceLastTireChange.length >= 200)
        aircraftDueForTireChanges.push(allAircraftData[i]);
    }
    // ERROR: you returned from the function in the first interation 
    // of the loop! you checked only one plane! 
    return aircraftDueForTireChanges;
  }
}

正确的原始代码就像我在代码段的代码

中所写的那样