0

I have array, lets call it fruit_shop, which contains a bunch of fruits. The fruits are objects with 3 params, name, unit_price, stock; which stands for the name of the fruit, stock in the shop and current price per unit. I need to find out which fruit has the maximum/minimum stock and which has the maximum value[unit_price*stock].

fruit_shop = [
  { name: "Apple",unit_price: 100, stock:5 },
  { name: "Mango",unit_price: 120, stock:4 },
  { name: "Orange",unit_price: 90, stock:6 }];

P.S. I am using javascript.

4
  • Hint: A simple approach would use just a for loop. Commented Oct 24, 2014 at 12:54
  • I was using underscore, so I used pluck on the whole thing and did a math.max/min.apply on the plucked arrays, and then find out which obj is the maximum by iterating through the array. Commented Oct 24, 2014 at 12:55
  • possible duplicate of Min and max in multidimensional array Commented Oct 24, 2014 at 12:55
  • What if two or more fruits have the same stock value? Commented Oct 24, 2014 at 13:00

3 Answers 3

1
var fruit_shop = [
    { name: "Apple", unit_price: 100, stock:5 },
    { name: "Mango", unit_price: 120, stock:4 },
    { name: "Orange", unit_price: 90, stock:6 }
];
var stock = fruit_shop.map(function(x){
    return x.stock;
});
var max = Math.max.apply(null, stock);
var maxfruit = fruit_shop.reduce(function(fruits, fruit){
    if(fruit.stock === max)
        fruits.push(fruit);
    return fruits;
}, []);

// Edit: I forgot about filter. Reduce is more general,
// but for collecting values, filter is simpler.
var maxfruit = fruit_shop.filter(function(fruit){
    return fruit.stock === max;
});

The remainder should be obvious.

And don't forget it's possible for more than one fruit to have, say, the minimum stock price.

Sign up to request clarification or add additional context in comments.

3 Comments

I used this kind of approach by using, _.pluck to create the stock. Now reading about the reduce part.
What is the use of function(fruits, fruit) If I directly try to return fruit , while using function(fruit) it gives error, cant read property of fruit.
fruits is the array containing all the fruits found that have the maximum value. fruit is the fruit object at each step in the iteration. See developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
0

One option is to sort array by unit_price and than the first one would be the max price and the last item is lowest price

fruit_shop.sort(function (a, b) {
 return b['unit_price'] - a['unit_price'];
});

max = fruit_shop[0];
min = fruit_shop[fruit_shop.length - 1];

Comments

0
var maxIdx = 0;
var i = 0;
for (var i = 0; i<fruit_shop.length;i++){
     if((fruit_shop[maxIdx].unit_price*fruit_shop[maxIdx].stock)<(fruit_shop[i].unit_price*fruit_shop[i].stock)){
        maxIdx = i;
     }
}
// now maxIdx contains index of the entry with highest unitprice*stock

solution by Alex G is valid, however sorting is at least O(n*log(n)) operation while a single loop is always O(n). Probably insignificant for small arrays, but can be very different for large ones

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.