gpt4 book ai didi

JavaScript:使用 Array 方法编写多个函数

转载 作者:行者123 更新时间:2023-11-30 16:16:44 25 4
gpt4 key购买 nike

我需要帮助将多个重复的函数写入一个传递变量的函数,而不是一遍又一遍地编写。

目前,我有一些 JavaScript 函数可以做同样的事情,只是使用不同的变量和字符串。

var Apple_Data = Info.Apple; // grabs string objects from JSON file
var Banana_Data = Info.Banana; // grabs string objects from JSON file
function CompareApple() {

if (counter == 0) {
prev_Apple_Data = Apple_Data;

}

if (Apple_Data > prev_Apple_Data) {
if ((Math.abs(Apple_Data - prev_Apple_Data) >= prev_Apple_Data / 2))
document.getElementById("Apple_img").src = "img/yelow.png",
StartCnt(1);
else if ((Math.abs(Apple_Data - prev_Apple_Data) <= prev_Apple_Data / 2))
document.getElementById("Apple_img").src = "img/green.png";

}

else if (Apple_Data < prev_Apple_Data) {
if ((Math.abs(Apple_Data - prev_Apple_Data) >= prev_Apple_Data / 2))
document.getElementById("Apple_img").src = "img/yelow.png",
StartCnt(1);
else if ((Math.abs(Apple_Data - prev_Apple_Data) <= prev_Apple_Data / 2))
document.getElementById("Apple_img").src = "img/green.png";
}

prev_Apple_Data = Apple_Data;
}

function CompareBanana() {

if (counter == 0) {
prev_Banana_Data = Banana_Data;

}

if (Banana_Data > prev_Banana_Data) {
if ((Math.abs(Banana_Data - prev_Banana_Data) >= prev_Banana_Data / 2))
document.getElementById("Banana_img").src = "img/yelow.png",
StartCnt(1);
else if ((Math.abs(Banana_Data - prev_Banana_Data) <= prev_Banana_Data / 2))
document.getElementById("Banana_img").src = "img/green.png";

}

else if (Banana_Data < prev_Banana_Data) {
if ((Math.abs(Banana_Data - prev_Banana_Data) >= prev_Banana_Data / 2))
document.getElementById("Banana_img").src = "img/yelow.png",
StartCnt(1);
else if ((Math.abs(Banana_Data - prev_Banana_Data) <= prev_Banana_Data / 2))
document.getElementById("Banana_img").src = "img/green.png";
}

prev_Banana_Data = Banana_Data;
}

如果我想将这些写在 1 个传递变量的函数中,我该怎么做?我在想这个过程会是这样的,必须制作 4 个不同的空数组

Var PreviousData = []; 
Var CurrentData = [];
Var DeclaredData = [];
Var ImageArray = [];

然后写一个函数传入这些数组

function Compare(PreviousData, CurrentData, DeclaredData, ImageArray){ 
if (counter ==0){
PreviousData = DeclaredData;
}
if (Declared_Data > PreviousData) {
if ((Math.abs(Declared_Data - PreviousData) >= PreviousData / 2))
document.getElementById("ImageArray").src = "img/yellow.png",
StartCnt(1);
else if ((Math.abs(Declared_Data - PreviousData) <= PreviousData / 2))
document.getElementById("ImageArray").src = "img/green.png";

}

else if (Declared_Data < PreviousData) {
if ((Math.abs(Declared_Data - PreviousData) >= PreviousData / 2))
document.getElementById("ImageArray").src = "img/yellow.png",
StartCnt(1);
else if ((Math.abs(Declared_Data - PreviousData) <= PreviousData / 2))
document.getElementById("ImageArray").src = "img/green.png";
}
}

计数器和 Math.abs 被写在那里以循环遍历对象并且当前有效。

然后通过类似的方式初始化函数

Compare(previousData, info.Apple, DeclaredData, ImageArray);

最佳答案

您可以将代码简化为:

var infoUpd = {
'prev': false
};

for (key in Info) {
infoUpd[ key ] = {
'data': Info[ key ],
'img': document.getElementById(key+"_img")
}
}


function compareStuff( key ) {
var stuffData = infoUpd[ key ];
if ( infoUpd.prev ) {
var calcResult = Math.abs(stuffData.data - infoUpd.prev) >= (infoUpd.prev / 2);
if (stuffData.data !== infoUpd.prev) {
if ( calcResult ) {
stuffData.img.src = "img/yelow.png";
} else {
stuffData.img.src = "img/green.png";
}
}
}
infoUpd.prev = stuffData.data;
}

compareStuff( 'Apple' );
compareStuff( 'Banana' );

关于JavaScript:使用 Array 方法编写多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35387900/

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