gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:52:36 25 4
gpt4 key购买 nike

我在下面的脚本中遇到的问题是,我在 oCollectedValue 数组中获得了完美的值,但由于某种原因,oCreatedOn 只收到推送到它的 1 个字符串,并且它是 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';
}

最佳答案

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

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

关于Javascript:array.push() 仅在 for 循环中推送源数组中的最后一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com