-1

Suppose we have an array

car = [
{
   id:355,
   wheels:3,
},
{
   id:3624,
   wheels:2,
},
{
   id:325,
   wheels:4,
},
{
   id:744,
   wheels:3,
},

How would I go about finding the minimum and maximum of the wheels property?

How I thought it should be done

My thoughts are we could extract the value of wheels from every object and store it in a new array then use the sort function to first sort the quantities in the (default) lexicographical order then just use array.sort((a,b) => a-b) to sort them in the ascending order after that I can use

const min = array[0]
const max = array[array.length-1]

at index 0 would be the min and length-1 would be the max. I am unable to get the value from the original array and store it in a new array also I would appreciate some different and/or easier methods.

Marios
  • 26,333
  • 8
  • 32
  • 52
  • The `Math.max` and `Math.min` functions are variadic. So here's an easy method: `Math.max(...car.map(c => c.wheels))` and `Math.min(...car.map(c => c.wheels))`. – CRice Aug 19 '20 at 19:30

4 Answers4

1

Ciao, basically you are right: calling sort function with custom sorting you could do something like:

car = [
{
   id:355,
   wheels:3,
},
{
   id:3624,
   wheels:2,
},
{
   id:325,
   wheels:4,
},
{
   id:744,
   wheels:3,
}];

car.sort(function(a, b) {
   return a.wheels - b.wheels;
})
console.log(car[0])  //min
console.log(car[car.length - 1]) //max

// store in new array
let result = []
result.push(car[0])
result.push(car[car.length - 1])    

console.log(result)
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
1

Use Array#reduce for it. Reduce starts with an initial value here [+inifinity, - infinity] and gives this value to the first parameter in the reduce statement. By using there [min, max] the values are stored immediately in these variables. Now the first element of your array will passed to the second parameter of the reduce function. Because I here used {wheels} only this property of the object will be passed to this variable.
With the arrow function => the whole parameter min, max and wheels we be transferred to an anonymous function which is here shortened because I only need the return-value.
Here is created an array with the min and max values from the accumulated min and max titll now and the new vaules from the actual car. The return value [min,max] is transported to the next step for the next array where the calculation starts again.
At the end you will receive the min, max from the hole array.

let car = [{id:355, wheels:3}, {id:3624, wheels:2}, {id:325, wheels:4}, {id:744,wheels:3}];

let result = car.reduce(([min,max], {wheels}) =>
    ([Math.min(min,wheels), Math.max(max,wheels)]),
    [Number.POSITIVE_INFINITY ,Number.NEGATIVE_INFINITY]);

console.log(result);
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

Try this:

let car = [
{
   id:355,
   wheels:3,
},
{
   id:3624,
   wheels:2,
},
{
   id:325,
   wheels:4,
},
{
   id:744,
   wheels:3,
}]
  
  console.log(Math.max.apply(Math, car.map(({wheels}) => wheels)))
  console.log(Math.min.apply(Math, car.map(({wheels}) => wheels)))
Marios
  • 26,333
  • 8
  • 32
  • 52
  • I'm running into an error with this that el is undefined – ProgrammingNoobH Aug 19 '20 at 19:53
  • @ProgrammingNoobHamza how is that possible? I edited my answer so you can run the code snippet from my answer itself. – Marios Aug 19 '20 at 19:56
  • You can shorten it a little: `Math.min.apply(Math, car.map(({wheels}) => wheels))` – Sascha Aug 19 '20 at 19:58
  • I may have made a mistake previously, you are right, your answer works and it is the simplest, thank you and I apologize. – ProgrammingNoobH Aug 19 '20 at 20:00
  • 1
    @Sascha thanks true! your answer is also clever! – Marios Aug 19 '20 at 20:02
  • @MariosKaramanis is there a way to get the id and console.log it of the car with the least amount of wheels? for example it showing me id:3624 in the output as car with id 3624 has 2 wheels – ProgrammingNoobH Aug 19 '20 at 23:31
  • @ProgrammingNoobH please this is a follow-up questions and stackoverflow does not allow us to answer. You will have to post another question and we will be able to answer it. Thanks – Marios Aug 19 '20 at 23:32
0

If you only need min and max values.

const min_and_max = (arr) => {
      let min = arr[0]
      let max = arr[0]
      for (const i in arr){
        if (arr[i].wheels > max.wheels){
          max = arr[i]
        } else if (arr[i].wheels < min.wheels){
          min = arr[i]
        }
      }
      return [min, max]
    }