匹配数组中的价格过滤器值并获取两个给定价格滑块值之间的价格值

我有两个数组,一个数组是价格滑块的值,其中它们是两个值,一个是起始值,另一个是滑块的结束值,我想检查滑块开始值和结束值之间的价格和值,请查看截图

http://img4.mukewang.com/64015a100001a1cc05720484.jpg

作为起始值和结束值的滑块代码


$(function() {

  $( "#slider-range" ).slider({

    range: true,

    min: 0,

    max: 200,

    values: [ 1, 50 ],

    slide: function( event, ui ) {

      $( "#amount" ).text( "$" + ui.values[ 0 ] + "-$" + ui.values[ 1 ] );

      collection.filterPrice(event, ui, getPriceTags());

    }

  });

  $( "#amount" ).text( "$1" + "-$" + $( "#slider-range" ).slider( "values", 1 ) );

});

出滑块

http://img3.mukewang.com/64015a1b0001ab2502080113.jpg

我想要的结果是


if slider value is from 10 to 50 so this checks the into the array

if any of the value is in between these 10 to 50 so it gives the result 20 50

or

if slider value is 10 to 60 so the results should be 20 50 and 50 to 100


慕勒3428872
浏览 114回答 3
3回答

拉丁的传说

(我希望这终于到了重点......)此代码返回范围与滑块选择的价格范围重叠(完全或部分)的所有价格段。/*&nbsp;* NOTES: price segments should not overlap&nbsp;*/const priceSegments = ['20-50', '51-100']; // originally was ['20-50', '50-100'] see noteconst sliderRange = [1, 50];const pricesList = [{&nbsp; &nbsp; value: 30.5,&nbsp; &nbsp; count: 2&nbsp; },&nbsp; {&nbsp; &nbsp; value: 27.7,&nbsp; &nbsp; count: 1&nbsp; },&nbsp; {&nbsp; &nbsp; value: 50.0,&nbsp; &nbsp; count: 2&nbsp; },&nbsp; {&nbsp; &nbsp; value: 55.5,&nbsp; &nbsp; count: 2&nbsp; }];// if you want an array of strings (e.g. ["20-50", "51-100"])const priceSegmentsMatchingRange_arrayOfStrings = priceSegments.filter(segment => {&nbsp; const [segmentMin, segmentMax] = segment.split('-');&nbsp; const [rangeMin, rangeMax] = sliderRange;&nbsp; return (&nbsp; &nbsp; (parseInt(segmentMin) < parseInt(rangeMin) && parseInt(segmentMax) >= parseInt(rangeMin)) ||&nbsp; &nbsp; (parseInt(segmentMin) >= parseInt(rangeMin) && parseInt(segmentMax) <= parseInt(rangeMax)) ||&nbsp; &nbsp; (parseInt(segmentMin) <= parseInt(rangeMax) && parseInt(segmentMax) > parseInt(rangeMax))&nbsp; );});console.log(priceSegmentsMatchingRange_arrayOfStrings);// if you want an array of arrays of strings (e.g. [["20", "50"], ["51", "100"]])const priceSegmentsMatchingRange_arrayArraysOfStrings = priceSegments.filter(segment => {&nbsp; const [segmentMin, segmentMax] = segment.split('-');&nbsp; const [rangeMin, rangeMax] = sliderRange;&nbsp; return (&nbsp; &nbsp; (parseInt(segmentMin) < parseInt(rangeMin) && parseInt(segmentMax) >= parseInt(rangeMin)) ||&nbsp; &nbsp; (parseInt(segmentMin) >= parseInt(rangeMin) && parseInt(segmentMax) <= parseInt(rangeMax)) ||&nbsp; &nbsp; (parseInt(segmentMin) <= parseInt(rangeMax) && parseInt(segmentMax) > parseInt(rangeMax))&nbsp; );}).map(segment => segment.split('-'));console.log(priceSegmentsMatchingRange_arrayArraysOfStrings);// if you want an array of arrays of numbers (e.g. [[20, 50], [51, 100]])const priceSegmentsMatchingRange_arrayArraysOfNumbers = priceSegments.filter(segment => {&nbsp; const [segmentMin, segmentMax] = segment.split('-');&nbsp; const [rangeMin, rangeMax] = sliderRange;&nbsp; return (&nbsp; &nbsp; (parseInt(segmentMin) < parseInt(rangeMin) && parseInt(segmentMax) >= parseInt(rangeMin)) ||&nbsp; &nbsp; (parseInt(segmentMin) >= parseInt(rangeMin) && parseInt(segmentMax) <= parseInt(rangeMax)) ||&nbsp; &nbsp; (parseInt(segmentMin) <= parseInt(rangeMax) && parseInt(segmentMax) > parseInt(rangeMax))&nbsp; );}).map(segment => segment.split('-').map(item => parseInt(item)));console.log(priceSegmentsMatchingRange_arrayArraysOfNumbers);

繁花如伊

你可以使用array.filter例如:allValues = [&nbsp; { val: 5, count: 2},&nbsp;&nbsp; { val: 6, count: 2},&nbsp;&nbsp; { val: 7, count: 2},&nbsp;&nbsp; { val: 8, count: 2},&nbsp;&nbsp; { val: 9, count: 2},&nbsp;&nbsp; { val: 10, count: 3}];&nbsp;&nbsp;var sliderStartVal = 6;var sliderEndVal = 9;&nbsp;&nbsp;console.log(allValues.filter(x => x.val > sliderStartVal && x.val < sliderEndVal));// Gives [{"val":7,"count":2},{"val":8,"count":2}]

富国沪深

如果我正确理解你想要实现的目标,你只需要使用Array.prototype.filter()(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)。[编辑] 我仍然不是 100% 确定我得到了你想要实现的目标,但据我所知,这个编辑过的片段可能是你需要的。/*&nbsp;* NOTES:&nbsp;* I think there are a couple of logical error I eliminated in this code snippet&nbsp;*&nbsp; &nbsp;1. price segments should not overlap&nbsp;*&nbsp; &nbsp;2. it should not be possible to select a price lower/higher than the lowes/highest price segment&nbsp;*/const priceSegments = ['20-50', '51-100']; // originally was ['20-50', '50-100'] see note (1.)const sliderRange = [20, 50]; // originally was [1, 50] - see note (2.)const pricesList = [{&nbsp; &nbsp; value: 30.5,&nbsp; &nbsp; count: 2&nbsp; },&nbsp; {&nbsp; &nbsp; value: 27.7,&nbsp; &nbsp; count: 1&nbsp; },&nbsp; {&nbsp; &nbsp; value: 50.0,&nbsp; &nbsp; count: 2&nbsp; },&nbsp; {&nbsp; &nbsp; value: 55.5,&nbsp; &nbsp; count: 2&nbsp; }];const priceRange = priceSegments.reduce((outObj, segment) => {&nbsp; /*&nbsp; &nbsp;* if the slider minimum falls within the segment,&nbsp; &nbsp;* set the minPrice to the segment minumum&nbsp; &nbsp;*/&nbsp; if (&nbsp; &nbsp; parseInt(segment.split('-')[0]) <= parseInt(sliderRange[0]) &&&nbsp; &nbsp; parseInt(segment.split('-')[1]) >= parseInt(sliderRange[0])&nbsp; ) {&nbsp; &nbsp; outObj.minPrice = parseInt(segment.split('-')[0]);&nbsp; }&nbsp; /*&nbsp; &nbsp;* if the slider maximum falls within the segment,&nbsp; &nbsp;* set the maxPrice to the segment maximum&nbsp; &nbsp;*/&nbsp; if (&nbsp; &nbsp; parseInt(segment.split('-')[0]) <= parseInt(sliderRange[1]) &&&nbsp; &nbsp; parseInt(segment.split('-')[1]) >= parseInt(sliderRange[1])&nbsp; ) {&nbsp; &nbsp; outObj.maxPrice = parseInt(segment.split('-')[1]);&nbsp; }&nbsp; return outObj;}, {&nbsp; minPrice: 0,&nbsp; maxPrice: 0});const {&nbsp; minPrice,&nbsp; maxPrice} = priceRange;const pricesInRange = pricesList.filter((price) => price.value >= minPrice && price.value <= maxPrice);console.log(pricesInRange);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript