如何获得价格x和y之间的条目?

时间:2013-02-11 17:17:44

标签: javascript jquery jquery-ui underscore.js

即时通讯使用当前的jquery ui滑块,其范围为: http://jqueryui.com/slider/#range

和underscore.js http://underscorejs.org/

因此,在用户停止滑动到某个功能后,我发送的最小值和最大值:

currentSlide:function(){
    $('#slider').slider({
        range: true,
        min: min,
        max: max,
        values: [ vmin, vmax ],
        stop:$.proxy(this.afterSlide,this)
    });
},

afterSlide:function(event, ui) {
    console.log(ui.values[0]);
},

在afterSlide函数中我得到了正确的最小值/最大值,但我不知道如何使用underscore.js来获得所有条目的价格从这个分钟开始并以最大值结束。

Examplearray:

var sample = {
    "response": {
        "things": [{
            "index": 0,
            "price": "10"
        },{
            "index": 1,
            "price": "15"
        },{
            "index": 2,
            "price": "60"
        },{
            "index": 3,
            "price": "10"
        },{
            "index": 4,
            "price": "100"
        }
        ]
    }
};

我使用Slider后我有最小/最大[12,61]所以我想看到所有条目只能以12到61美元之间的价格开始

var result = _.find(sample.response.things, min/max);??

我没有正确理解_.range()函数以使用它我需要的方式,或使用 .where / .find()函数创建正确的语句。

1 个答案:

答案 0 :(得分:2)

我只是得到最大值和最小值,因为这看起来相当简单,我只是迭代对象并检查相关值是否在范围内,并将其添加到新对象,真的不需要_underscore ,像这样:

afterSlide:function(event, ui) {
   var min = ui.values[0],
       max = ui.values[1],
     items = {};

    $.each(sample.response.things[0], function(key, val) {
        if (val.price > min && val.price < max) {
            items[val.index] = val.price;
        }
    })
},