0

我有一个定价数组

pricing = new Array();

pricing[1] = 35;
pricing[2] = 60;
pricing[3] = 84;
pricing[4] = 104;
pricing[5] = 120;
pricing[6] = 132;
pricing[7] = 140;
pricing[8] = 144;
pricing[9] = 153;
pricing[10] = 160;

10以下的都有价格,10以上的都和10一样

它只到 20,所以我最初所做的只是重复 11 - 20 的价格。但这很浪费,我怎么能告诉我数组所有内容 > 10 = 160

ps我的最终版本是浓缩的:)

4

5 回答 5

2

你可以让你的数组保持原样并使用如下函数:

var getPrice = function(arr, index){
    return arr[index > 10 ? 10 : index];
}
于 2013-06-07T13:11:54.163 回答
1
var pricing = [], i;
pricing.push(35);
pricing.push(60);
pricing.push(84);
pricing.push(104);
pricing.push(120);
pricing.push(132);
pricing.push(140);
pricing.push(144);
pricing.push(153);
for (i = 0; i < 11; i++) {
    pricing.push(160);
}

我还为此制作了一个JSFiddle

@CD 声明push 函数可以将多个项目附加到数组中。代码将如下所示:

var pricing = [], value = 160;
pricing.push(35);
pricing.push(60);
pricing.push(84);
pricing.push(104);
pricing.push(120);
pricing.push(132);
pricing.push(140);
pricing.push(144);
pricing.push(153);
pricing.push(value, value, value, value, value, value, value, value, value, value, value);
于 2013-06-07T13:17:35.177 回答
0
i=10;
while(i--) { pricing[i+10] = 160; }
于 2013-06-07T13:11:31.840 回答
0
pricing = new Array();
var arraySize = 100;

pricing[1] = 35;
pricing[2] = 60;
pricing[3] = 84;
pricing[4] = 104;
pricing[5] = 120;
pricing[6] = 132;
pricing[7] = 140;
pricing[8] = 144;
pricing[9] = 153;

for(var x = 10; x < arraySize; x++)
    pricing[x] = 160

console.log(pricing);
于 2013-06-07T13:12:00.267 回答
0

您错过了数组中的第一个条目(因为它是基于 0 的)。我会把它改成这样:

pricing = new Array(20);

pricing[0] = 35;
pricing[1] = 60;
pricing[2] = 84;
pricing[3] = 104;
pricing[4] = 120;
pricing[5] = 132;
pricing[6] = 140;
pricing[7] = 144;
pricing[8] = 153;
pricing[9] = 160;

然后您可以使用以下方法设置最后 10 个:

for(var x = 10; x < pricing.length; x++) {
    pricing[x] = pricing[9];
}

示例小提琴

于 2013-06-07T13:14:08.907 回答