caramba caramba - 2 months ago 10
Javascript Question

Javascript get value from object based on key compared to its size smaller then next key

Trying to calculate shipping prices based on the weight of a package.

I have an Object containing prices for a given weight like

shippingPrices = {
'economy': {
[weight]: price,
. . .
}
};


Then I want to get the right shipping price with a function passing the weight of a package like:

addShippingPrice(700); // 700 is weight in grams


I've tried like so:



shippingPrices = {
'economy': {
2000: 7,
5000: 9,
10000: 10,
20000: 15,
30000: 22
}
};

var addShippingPrice = function(weight) {
var price = 0;
for( var maxWeight in shippingPrices.economy) {
maxWeight = parseInt(maxWeight, 10);

console.log(shippingPrices.economy[maxWeight]);

if(weight < maxWeight) {
// return price = shippingPrices.economy[maxWeight];
}
}
console.log('amount', price);
};

addShippingPrice(700);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





The "right" shipping price in this case would be
7
and if I call the function with weight 8000
addShippingPrice(8000);
it should return
10


How can I achieve that? Also if it would be better to change the shippingPrices Object to Array I'll change it!

Answer

You could use an iterable structure, like

var shippingPrices = {
    economy: [
        { weight: 2000, price: 7 },
        { weight: 5000, price: 9 },
        { weight: 10000, price: 10 },
        { weight: 20000, price: 15 },
        { weight: 30000, price: 22 }
    ],
    priority: [
        { weight: 2000, price: 9 },
        { weight: 5000, price: 11 },
        { weight: 10000, price: 12 },
        { weight: 20000, price: 18 },
        { weight: 30000, price: 25 }
    ]
};

function getShippingPrice(type, weight) {
    var price;
    shippingPrices[type].some(function (a) {
        if (a.weight >= weight) {
            price = a.price;
            return true;
        }
    });
    return price;
}

var shippingPrices = { economy: [{ weight: 2000, price: 7 }, { weight: 5000, price: 9 }, { weight: 10000, price: 10 }, { weight: 20000, price: 15 }, { weight: 30000, price: 22 }], priority: [{ weight: 2000, price: 9 }, { weight: 5000, price: 11 }, { weight: 10000, price: 12 }, { weight: 20000, price: 18 }, { weight: 30000, price: 25 }] };

console.log(getShippingPrice('economy', 700));
console.log(getShippingPrice('economy', 8000));

ES6

function getShippingPrice(type, weight) {
    return (shippingPrices[type].find(a => a.weight >= weight) || {}).price;
}

var shippingPrices = { economy: [{ weight: 2000, price: 7 }, { weight: 5000, price: 9 }, { weight: 10000, price: 10 }, { weight: 20000, price: 15 }, { weight: 30000, price: 22 }], priority: [{ weight: 2000, price: 9 }, { weight: 5000, price: 11 }, { weight: 10000, price: 12 }, { weight: 20000, price: 18 }, { weight: 30000, price: 25 }] };

console.log(getShippingPrice('economy', 700));
console.log(getShippingPrice('economy', 8000));