Backbone JS找到最小值

chc*_*ist 1 backbone.js underscore.js

我有这个json填充我TableListCollection的模型集合()TableModel)

{
    "tables": [
        {
            "tableId": 15,
            "size": 8,
            "occupiedSeats": 0,
            "stakes": {
                "smallBlindAmount": 10,
                "bigBlindAmount": 20,
                "minBuyInAmount": 20,
                "maxBuyInAmount": 200
            },
            "gameType": "HOLDEM",
            "gameSpeed": "NORMAL",
            "friends": []
        },
        {
            "tableId": 16,
            "size": 8,
            "occupiedSeats": 0,
            "stakes": {
                "smallBlindAmount": 20,
                "bigBlindAmount": 40,
                "minBuyInAmount": 20,
                "maxBuyInAmount": 200
            },
            "gameType": "HOLDEM",
            "gameSpeed": "NORMAL",
            "friends": []
        },
        {
            "tableId": 17,
            "size": 8,
            "occupiedSeats": 0,
            "stakes": {
                "smallBlindAmount": 40,
                "bigBlindAmount": 60,
                "minBuyInAmount": 20,
                "maxBuyInAmount": 200
            },
            "gameType": "HOLDEM",
            "gameSpeed": "NORMAL",
            "friends": []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想找到最小的桌子smallBlindAmount.

我看到我可以使用_.min()但我无法弄清楚我必须作为迭代器传递什么.

提前致谢.

nik*_*shr 6

直接在JSON上

var json=...
var min = _.min(json.tables,function(item) {
    return item.stakes.smallBlindAmount
});
console.log(min.stakes.smallBlindAmount);
Run Code Online (Sandbox Code Playgroud)

或者你的收藏品

var json=...
var c=new Backbone.Collection(json.tables);
var m=c.min(function(model) {
    return model.get("stakes").smallBlindAmount
});
console.log(m.get("stakes").smallBlindAmount);
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,迭代器都用于提取要比较的值.