2

1 つ購入する場合、各価格は $100 です。購入 >= 2 の場合、各価格は $90 です。購入 >=5 の場合、各価格は $80 です。購入 >=8 の場合、各価格は 70 です........

json データは dynamicです。つまり、どの要素が 1 か 2 か 3 か、またはそれ以外かということです。したがって、price_qty の値がどうなるかわかりません。

[{"price_id":"1","website_id":"0","price_qty":2,"price":"90.0000"},
 {"price_id":"2","website_id":"0","price_qty":5,"price":"80.0000"},
 {"price_id":"3","website_id":"0","price_qty":8,"price":"70.0000"}]

訪問者が購入する製品の数量を持つ結果という名前の変数があると仮定します。

var result.

今、私がやりたいこと: 結果を json データ (price_qty) と比較し、全体の価格を出力します。例: if(result >2) { write the qty is +result, the price is + price.}. コードの書き方は?

一度私が使用した:

var myObject= the json data,
jQuery.each(myObject, function(_index, _item){ 
var objectInArray = _item; 
if(objectInArray.price_qty  result)

...

それから私は左を行う方法がわかりません。

4

1 に答える 1

1
// first sort the array
var sorted = arrayWithJsonData.sort(function(a,b){return a.price_qty - b.price_qty;});

// find the nearest price qty
var i=0;
while(i < sorted.length && sorted[i].price_qty <= result){i++;} 

var price = sorted[i-1].price;
于 2012-11-22T10:35:57.783 に答える