亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

匹配數組中的價格過濾器值并獲取兩個給定價格滑塊值之間的價格值

匹配數組中的價格過濾器值并獲取兩個給定價格滑塊值之間的價格值

慕勒3428872 2023-03-03 10:22:38
我有兩個數組,一個數組是價格滑塊的值,其中它們是兩個值,一個是起始值,另一個是滑塊的結束值,我想檢查滑塊開始值和結束值之間的價格和值,請查看截圖作為起始值和結束值的滑塊代碼$(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 ) );});出滑塊我想要的結果是if slider value is from 10 to 50 so this checks the into the arrayif any of the value is in between these 10 to 50 so it gives the result 20 50orif slider value is 10 to 60 so the results should be 20 50 and 50 to 100
查看完整描述

3 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

(我希望這終于到了重點......)


此代碼返回范圍與滑塊選擇的價格范圍重疊(完全或部分)的所有價格段。


/*

 * NOTES: price segments should not overlap

 */


const priceSegments = ['20-50', '51-100']; // originally was ['20-50', '50-100'] see note

const sliderRange = [1, 50];

const pricesList = [{

    value: 30.5,

    count: 2

  },

  {

    value: 27.7,

    count: 1

  },

  {

    value: 50.0,

    count: 2

  },

  {

    value: 55.5,

    count: 2

  }

];


// if you want an array of strings (e.g. ["20-50", "51-100"])

const priceSegmentsMatchingRange_arrayOfStrings = priceSegments.filter(segment => {

  const [segmentMin, segmentMax] = segment.split('-');

  const [rangeMin, rangeMax] = sliderRange;

  return (

    (parseInt(segmentMin) < parseInt(rangeMin) && parseInt(segmentMax) >= parseInt(rangeMin)) ||

    (parseInt(segmentMin) >= parseInt(rangeMin) && parseInt(segmentMax) <= parseInt(rangeMax)) ||

    (parseInt(segmentMin) <= parseInt(rangeMax) && parseInt(segmentMax) > parseInt(rangeMax))

  );

});

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 => {

  const [segmentMin, segmentMax] = segment.split('-');

  const [rangeMin, rangeMax] = sliderRange;

  return (

    (parseInt(segmentMin) < parseInt(rangeMin) && parseInt(segmentMax) >= parseInt(rangeMin)) ||

    (parseInt(segmentMin) >= parseInt(rangeMin) && parseInt(segmentMax) <= parseInt(rangeMax)) ||

    (parseInt(segmentMin) <= parseInt(rangeMax) && parseInt(segmentMax) > parseInt(rangeMax))

  );

}).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 => {

  const [segmentMin, segmentMax] = segment.split('-');

  const [rangeMin, rangeMax] = sliderRange;

  return (

    (parseInt(segmentMin) < parseInt(rangeMin) && parseInt(segmentMax) >= parseInt(rangeMin)) ||

    (parseInt(segmentMin) >= parseInt(rangeMin) && parseInt(segmentMax) <= parseInt(rangeMax)) ||

    (parseInt(segmentMin) <= parseInt(rangeMax) && parseInt(segmentMax) > parseInt(rangeMax))

  );

}).map(segment => segment.split('-').map(item => parseInt(item)));

console.log(priceSegmentsMatchingRange_arrayArraysOfNumbers);


查看完整回答
反對 回復 2023-03-03
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

你可以使用array.filter

例如:

allValues = [

  { val: 5, count: 2}, 

  { val: 6, count: 2}, 

  { val: 7, count: 2}, 

  { val: 8, count: 2}, 

  { val: 9, count: 2}, 

  { val: 10, count: 3}

];

  

var sliderStartVal = 6;

var sliderEndVal = 9;

  

console.log(allValues.filter(x => x.val > sliderStartVal && x.val < sliderEndVal));


// Gives [{"val":7,"count":2},{"val":8,"count":2}]


查看完整回答
反對 回復 2023-03-03
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

如果我正確理解你想要實現的目標,你只需要使用Array.prototype.filter()(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)。


[編輯] 我仍然不是 100% 確定我得到了你想要實現的目標,但據我所知,這個編輯過的片段可能是你需要的。


/*

 * NOTES:

 * I think there are a couple of logical error I eliminated in this code snippet

 *   1. price segments should not overlap

 *   2. it should not be possible to select a price lower/higher than the lowes/highest price segment

 */


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 = [{

    value: 30.5,

    count: 2

  },

  {

    value: 27.7,

    count: 1

  },

  {

    value: 50.0,

    count: 2

  },

  {

    value: 55.5,

    count: 2

  }

];


const priceRange = priceSegments.reduce((outObj, segment) => {

  /*

   * if the slider minimum falls within the segment,

   * set the minPrice to the segment minumum

   */

  if (

    parseInt(segment.split('-')[0]) <= parseInt(sliderRange[0]) &&

    parseInt(segment.split('-')[1]) >= parseInt(sliderRange[0])

  ) {

    outObj.minPrice = parseInt(segment.split('-')[0]);

  }


  /*

   * if the slider maximum falls within the segment,

   * set the maxPrice to the segment maximum

   */

  if (

    parseInt(segment.split('-')[0]) <= parseInt(sliderRange[1]) &&

    parseInt(segment.split('-')[1]) >= parseInt(sliderRange[1])

  ) {

    outObj.maxPrice = parseInt(segment.split('-')[1]);

  }

  return outObj;

}, {

  minPrice: 0,

  maxPrice: 0

});


const {

  minPrice,

  maxPrice

} = priceRange;

const pricesInRange = pricesList.filter((price) => price.value >= minPrice && price.value <= maxPrice);


console.log(pricesInRange);


查看完整回答
反對 回復 2023-03-03
  • 3 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號