javascript - Find Index In An Array Range -
i have array of quantities , find in array number found. if user entered 16 function return between index 6 & 7 because 16 greater 15 less 20.
in wider picture index used in lookup find price band desired quantity.
edit
html markup
<table cellpadding="0" cellspacing="0" border="0"> <tbody><tr> <th scope="row">quantity</th> <th id="js-quantityid-0" scope="col">0</th> <th id="js-quantityid-1" scope="col">1</th> <th id="js-quantityid-2" scope="col">2</th> <th id="js-quantityid-3" scope="col">3</th> <th id="js-quantityid-4" scope="col">4</th> <th id="js-quantityid-5" scope="col">5</th> <th id="js-quantityid-6" scope="col">10</th> <th id="js-quantityid-7" scope="col">15</th> <th id="js-quantityid-8" scope="col">20</th> <th id="js-quantityid-9" scope="col">30</th> <th id="js-quantityid-10" scope="col">40</th> <th id="js-quantityid-11" scope="col">50</th> <th id="js-quantityid-12" scope="col">60</th> </tr> <tr> <th scope="row">price (inc. vat)</th> <td id="js-priceid-0">0.00</td> <td id="js-priceid-1">45.60</td> <td id="js-priceid-2">38.40</td> <td id="js-priceid-3">32.40</td> <td id="js-priceid-4">34.56</td> <td id="js-priceid-5">30.72</td> <td id="js-priceid-6">26.80</td> <td id="js-priceid-7">21.60</td> <td id="js-priceid-8">20.60</td> <td id="js-priceid-9">17.60</td> <td id="js-priceid-10">16.60</td> <td id="js-priceid-11">15.60</td> <td id="js-priceid-12">14.00</td> </tr> </tbody> </table>
working script
function gettablecontents() { var productarray = []; jquery("th[id^='js-quantityid-']").each(function () { var n = jquery(this).attr("id").replace('js-quantityid-', ''); // number id productarray.push({ qty: parseint(jquery(this).text()), price: parsefloat(jquery('#js-priceid-' + n).text().replace('£','')) }); }); return productarray = cleanarray(productarray); }
answer
the problem due getting quantity string, upon parsing integer fixed issue @michael geary
it's not idea set parallel arrays matching elements price
, quantity
arrays in example. error-prone , hard visually match price goes quantity.
instead, should use single array each array element object combines quantity level , price:
var pricebands = [ { qty: 1, price: '£45.60' }, { qty: 2, price: '£38.40' }, { qty: 3, price: '£32.40' }, { qty: 4, price: '£32.10' }, { qty: 5, price: '£34.56' }, { qty: 10, price: '£30.72' }, { qty: 15, price: '£26.80' }, { qty: 20, price: '£21.60' }, { qty: 30, price: '£21.00' }, { qty: 40, price: '£21.00' } ];
now it's easy see price goes quantity, , there's no question of having same number of elements in both arrays.
(btw i'm curious why price goes up @ 5 items!)
if notation seems tedious, can streamline it:
var pricebands = makepricebands([ ' 1: £45.60', ' 2: £38.40', ' 3: £32.40', ' 4: £32.10', ' 5: £34.56', '10: £30.72', '15: £26.80', '20: £21.60', '30: £21.00', '40: £21.00', ]);
that produces same array of objects listed previously, using function convert more concise notation:
function makepricebands( bandsin ) { var bands = []; for( var = 0; < bandsin.length; ++i ) { var band = bandsin[i].split(/: */)); bands.push({ qty: +band[0], price: band[1] }); } return bands; }
for modern browsers write little more simply:
function makepricebands( bands ) { return bands.map( function( b ) { var band = b.split(/: */)); return { qty: +band[0], price: band[1] }; }); }
but first version work on old browser.
either way, code find proper price simple. walk backwards through array until reach element your input quantity greater or equal quantity in array element.
we'll make function expects prices
array parameter, can use different products passing in different arrays.
also, didn't specify in question when input quantity equal 1 of price level quantities, made assumption that, example, if have 10 or more, price level quantity 10. (in other words, don't have buy 11 quantity 10 price.)
function priceforquantity( prices, n ) { for( var = prices.length - 1; >= 0; --i ) { if( n >= prices[i].qty ) { return prices[i].price; } } return '£0.00'; }
or walk forward through array, find easier think particular problem walking backwards.
you can test code few example quantities, making sure test border conditions of 1 greater , 1 less particular quantity level:
function test( n ) { console.log( n, priceforquantity( pricebands, n ) ); } test( 0 ); test( 1 ); test( 3 ); test( 5 ); test( 6 ); test( 9 ); test( 10 ); test( 39 ); test( 40 ); test( 41 );
this log:
0 "£0.00" 1 "£45.60" 3 "£32.40" 5 "£34.56" 6 "£34.56" 9 "£34.56" 10 "£30.72" 39 "£21.00" 40 "£21.00" 41 "£21.00"
Comments
Post a Comment