1

I have json object of which I would like to get the min price. Below is the response.

[
  {
    "room": {
      "price": 217,
      "available": true
    }
  },
  {
    "room": {
      "price": 302,
      "available": true,
    }
  },
  {
    "room": {
      "price": 427,
      "available": true,
    }
  }
]

I have tried a solution from Stackoverflow but it won't work in case.

  var arr = Object.keys( response ).map(function ( key ) { return response[key]; });
  var min = Math.min.apply( null, arr );

Please help

Community
  • 1
  • 1
Red Virus
  • 1,633
  • 3
  • 25
  • 34

6 Answers6

4

You can try this:

let response = [
  {
    "room": {
      "price": 217,
      "available": true
    }
  },
  {
    "room": {
      "price": 302,
      "available": true,
    }
  },
  {
    "room": {
      "price": 427,
      "available": true,
    }
  }
];
let values  = response.map(function(v) {
  return v.room.price;
});
var min = Math.min.apply( null, values );
console.log(min)

using ES2015 you can also make it in one line:

var min = Math.min.apply( null, response.map((v) => v.room.price));
Bartek Fryzowicz
  • 6,464
  • 18
  • 27
1

You have array not object so you can't use Object.keys(). You can also use spread syntax like this.

var data = [{
  "room": {
    "price": 217,
    "available": true
  }
}, {
  "room": {
    "price": 302,
    "available": true,
  }
}, {
  "room": {
    "price": 427,
    "available": true,
  }
}]

var min = Math.min(...data.map(e => e.room.price))
console.log(min)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

You can use Array.protype.reduce()

var rooms = [{
    "room": {
      "price": 217,
      "available": true
    }
  },
  {
    "room": {
      "price": 302,
      "available": true,
    }
  },
  {
    "room": {
      "price": 427,
      "available": true,
    }
  }
];
console.log(rooms.reduce((prev, curr) => prev.price > curr.price ? curr : prev).room.price);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
1
        var response = [
            {
                "room": {
                    "price": 217,
                    "available": true
                }
            },
            {
                "room": {
                    "price": 302,
                    "available": true,
                }
            },
            {
                "room": {
                    "price": 427,
                    "available": true,
                }
            }
        ];

        debugger;
        if (response && response.length > 0) 
        {
            var min = response[0].room.price;

            for (var i = 0; i < response.length; i++)           
                if (response[i].room.price < min)
                    min = response[i].room.price;

            console.log(min);
        }
0

native JS solution:

var t =[ { "room": { "price": 217, "available": true } }, { "room": { "price": 302, "available": true, } }, { "room": { "price": 427, "available": true, } } ]
var min = t.map(function(el){return el.room.price}).reduce(function(el){return Math.min(el)});

working fiddle

lbrutti
  • 1,181
  • 13
  • 19
0

lbrutty code is a bit wrong and function will always return first element. So there is a little fix.

var t = [ { "room": { "price": 300, "available": true } }, { "room": { "price": 102, "available": true, } }, { "room": { "price": 427, "available": true, } } ];
var min = t.map(function(el){return el.room.price}).reduce(function(prevEl, el){return Math.min(prevEl, el)});

https://jsfiddle.net/xe0dcoym/17/

Yura Kosyak
  • 401
  • 3
  • 16