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
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
Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Feb 23, 2017 at 10:38 Red VirusRed Virus 1,7073 gold badges26 silver badges36 bronze badges 3-
what is the
response
object? – Amresh Venugopal Commented Feb 23, 2017 at 10:39 - @AmreshVenugopal the json I pasted above is the response – Red Virus Commented Feb 23, 2017 at 10:40
- 2 At first you need to convert your json to js array using JSON.parse – styopdev Commented Feb 23, 2017 at 10:40
6 Answers
Reset to default 4You 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));
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)
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);
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);
}
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
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/xe0dcoym/17/