简体   繁体   English

从嵌套的json对象获取最小值

[英]Get min value from a nested json object

I have json object of which I would like to get the min price. 我有想要获取最低价格的json对象。 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. 我已经尝试过Stackoverflow的解决方案,但万一无法使用。

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

Please help 请帮忙

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: 使用ES2015,您还可以将其制作成一行:

var min = Math.min.apply( null, response.map((v) => v.room.price));

You have array not object so you can't use Object.keys() . 您没有数组对象,所以不能使用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() 您可以使用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: 本机JS解决方案:

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. lbrutty代码有点错误,函数将始终返回第一个元素。 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/ https://jsfiddle.net/xe0dcoym/17/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM