Javascript:array.push()仅在for循环中推送源数组中的最后一个字符串

时间:2018-07-13 18:22:55

标签: javascript arrays

我在下面的脚本中遇到的问题是,我在oCollectedValue数组中获得了完美的值,但是由于某种原因,oCreatedOn只得到了一个压入该字符串的字符串,而最后一个字符串压入了CreatedOn数组。对于我的一生,我看不到这段代码有什么问题。请帮助。谢谢!

Route = "";
    var UpperCoherenceLimit = 9.99;
    var LowerCoherenceLimit = 4.98;
    var CollectedValue = ["1.7865","3.7865","4.786532564000","5.7865","6.7865","7.7865"];
    var CreatedOn = ["7/1/2018 12:00:00 AM","7/2/2018 12:00:00 AM","7/3/2018 12:00:00 AM","7/4/2018 12:00:00 AM","7/5/2018 12:00:00 AM","7/7/2018 12:00:00 AM"];
    var SkippedValueList = [];
    var oCollectedValue = [];
    var oCreatedOn = [];
    var DisableCoherenceError = true;

    for (var i = 0; i < CollectedValue.length; i++)
    {
        if(CollectedValue[i] > UpperCoherenceLimit || CollectedValue[i] < LowerCoherenceLimit)
        {
            if(UpperCoherenceLimit != LowerCoherenceLimit)
            {
                if(DisableCoherenceError == false)
                {
                    SkippedValueList.push(CollectedValue[i])
                }
                else
                {
                    SkippedValueList.push(CollectedValue[i])
                }
            }
        }
        else
        {
            oCollectedValue.push(CollectedValue[i])
            oCreatedOn.push(CreatedOn[i])
        }
    }

    if (oCollectedValue.length == 0)
    {
        Route = 'SKIP';
    }

2 个答案:

答案 0 :(得分:2)

仅正确分隔数组项似乎可以解决问题?比较数字和字符串时,Javascript会自动为您进行类型转换,但是您也应该考虑将字符串解析为浮点数。

    var UpperCoherenceLimit = 9.99;
    var LowerCoherenceLimit = 4.98;
    var CollectedValue = ["1.7865", "3.7865", "4.786532564000", "5.7865", "6.7865", "7.7865"];
    var CreatedOn = ["7/1/2018 12:00:00 AM", "7/2/2018 12:00:00 AM", "7/3/2018 12:00:00 AM", "7/4/2018 12:00:00 AM", "7/5/2018 12:00:00 AM", "7/7/2018 12:00:00 AM"];
    var SkippedValueList = [];
    var oCollectedValue = [];
    var oCreatedOn = [];
    var DisableCoherenceError = true;

    for (var i = 0; i < CollectedValue.length; i++)
    {
        if(CollectedValue[i] > UpperCoherenceLimit || CollectedValue[i] < LowerCoherenceLimit)
        {
            if(UpperCoherenceLimit != LowerCoherenceLimit)
            {
                SkippedValueList.push(CollectedValue[i])
            }
        }
        else
        {
            oCollectedValue.push(CollectedValue[i])
            oCreatedOn.push(CreatedOn[i])
        }
    }
    
    console.log(oCollectedValue);
    console.log(oCreatedOn);

答案 1 :(得分:0)

以下测试通过:

require('chai').should();

describe('Arrays', () => {
  it('needs commas', () => {
    Route = "";
    var UpperCoherenceLimit = 9.99;
    var LowerCoherenceLimit = 4.98;
    var CollectedValue = ["1.7865", "3.7865", "4.786532564000", "5.7865", "6.7865", "7.7865"];
    var CreatedOn = ["7/1/2018 12:00:00 AM", "7/2/2018 12:00:00 AM", "7/3/2018 12:00:00 AM", "7/4/2018 12:00:00 AM", "7/5/2018 12:00:00 AM", "7/7/2018 12:00:00 AM"];
    var SkippedValueList = [];
    var oCollectedValue = [];
    var oCreatedOn = [];
    var DisableCoherenceError = true;

    for (var i = 0; i < CollectedValue.length; i++) {
      if (CollectedValue[i] > UpperCoherenceLimit || CollectedValue[i] < LowerCoherenceLimit) {
        if (UpperCoherenceLimit != LowerCoherenceLimit) {
          if (DisableCoherenceError == false) {
            SkippedValueList.push(CollectedValue[i])
          }
          else {
            SkippedValueList.push(CollectedValue[i])
          }
        }
      }
      else {
        oCollectedValue.push(CollectedValue[i])
        oCreatedOn.push(CreatedOn[i])
      }
    }

    if (oCollectedValue.length == 0) {
      Route = 'SKIP';
    }
    oCreatedOn.length.should.equal(3);
  });
});

因此,您发布的代码不能反映您面临的问题。请给我们一个Minimal, Complete, and Verifiable Example