gpt4 book ai didi

javascript - 从 jquery/javascript 函数获取数组

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

我有一个 JavaScript 函数,它生成并返回一个新数组(数组):

function getFees(id){

var prep = new Array, primary = new Array, secondary = new Array, vce = new Array;

prep[0] = 733;
primary[0] = 792;
secondary[0] = 879;
vce[0] = 1108;

if (id == 2) {

prep[1] = (prep[0] - prep[0] * 5 / 100);
prep[1] = Math.ceil(prep[1]);

primary[1] = (primary[0] - primary[0] * 5 / 100);
primary[1] = Math.ceil(primary[1]);

secondary[1] = (secondary[0] - secondary[0] * 5 / 100);
secondary[1] = Math.floor(secondary[1]);

vce[1] = (vce[0] - vce[0] * 5 / 100);
vce[1] = Math.floor(vce[1]);

} else if (id == 3) {

prep[2] = (prep[0] - prep[0] * 10 / 100);
prep[2] = Math.ceil(prep[2]);

primary[2] = (primary[0] - primary[0] * 10 / 100);
primary[2] = Math.ceil(primary[2]);

secondary[2] = (secondary[0] - secondary[0] * 10 / 100);
secondary[2] = Math.floor(secondary[2]);

vce[2] = (vce[0] - vce[0] * 10 / 100);
vce[2] = Math.floor(vce[2]);

} else if (id == 4) {

prep[3] = (prep[0] - prep[0] * 50 / 100);
prep[3] = Math.ceil(prep[3]);

primary[3] = (primary[0] - primary[0] * 50 / 100);
primary[3] = Math.ceil(primary[3]);

secondary[3] = (secondary[0] - secondary[0] * 50 / 100);
secondary[3] = Math.ceil(secondary[3]);

vce[3] = (vce[0] - vce[0] * 50 / 100);
vce[3] = Math.floor(vce[3]);

} else if (id >= 5) {

prep[4] = (prep[0] - prep[0] * 75 / 100);
prep[4] = Math.floor(prep[4]);

primary[4] = (primary[0] - primary[0] * 75 / 100);
primary[4] = Math.ceil(primary[4]);

secondary[4] = (secondary[0] - secondary[0] * 75 / 100);
secondary[4] = Math.ceil(secondary[4]);

vce[4] = (vce[0] - vce[0] * 75 / 100);
vce[4] = Math.floor(vce[4]);

}

var newArray = [];

newArray.push({'prep':prep}); //prep array = 733,697
newArray.push({'primary':primary}); //primary array = 792,753
newArray.push({'secondary':secondary}); //secondary array = 879,835
newArray.push({'vce':vce}); //vce array = 1108,1052

return newArray;
}

基本上我已经在底部的 .push 部分给出了一个示例。然后我通过这样做来调用我的函数:

var fees = getFees(2);
alert(fees);

哪个警报:

[object Object],[object Object],[object Object],[object Object]

如果我这样做:

alert(fees.toSource());

我明白了:

[{prep:[733, 697]}, {primary:[792, 753]}, {secondary:[879, 835]}, {vce:[1108, 1052]}]

我需要做的是从任何项目(准备/主要/次要/vce)中获取编号,例如..

fees.prep[0];
fees.primary[1];

但是当我尝试这样做时,我收到此错误:

类型错误:fees.prep 未定义

我错过了什么?任何帮助将不胜感激!! :)

最佳答案

你需要这样访问

fees[0].prep[0];

关于javascript - 从 jquery/javascript 函数获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44252525/

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