I have below array
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200]
From the frontEnd user enters any number say it is
const number = 136
I need to find closest to the number but the lesser one.
So the output should be 125
Even if the number is 149
the output should be 125
How can I do this. I tried many way but could get the answer.
Thanks!!!
答案 0 :(得分:1)
If it's sorted in ascending order like in the question this should work.
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200];
const number = 136;
function findClosest(arr, num) {
for (let i = 0; i < arr.length; ++i) {
if (arr[i] > num) {
return arr[i - 1];
}
}
}
console.log(findClosest(floorPerDayMilestones,number));
答案 1 :(得分:1)
You can use Array.reduce
for this
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200]
function getClosestNumber(d) {
return floorPerDayMilestones.reduce((a, b) => b <=d && a < b ? b : a, 0 )
}
console.log(getClosestNumber(135) || 'No lesser number available')
console.log(getClosestNumber(149) || 'No lesser number available')
console.log(getClosestNumber(22) || 'No lesser number available')
答案 2 :(得分:1)
Maybe you should look at this: get closest number out of array
And in your foreach, save your closest number in a var. Then check if your number is bigger than the one in your array . if yes, take your last var, else continue your foreach
答案 3 :(得分:1)
Try this
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200];
const number = 136;
const nextLesser = floorPerDayMilestones.reduce((nl, curr) => (curr <= number) && (curr > nl) ? curr : nl , 0)
console.log(nextLesser)
答案 4 :(得分:1)
You can sort the array in ascending order and then find the index of number that is immediately higher than number
then one position less of that value will be the immediate number that is less than number
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200]
const number = 136;
floorPerDayMilestones.sort((a,b)=> a-b);
var index = floorPerDayMilestones.findIndex(val => val>number);
var num = floorPerDayMilestones[index-1];
console.log(num);
答案 5 :(得分:1)
您还可以过滤出大于给定的数字,并从该子集中选择最大值。
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200]
const number = 136
const filtered = floorPerDayMilestones.filter(el=>el<=number);
console.log(Math.max(...filtered))
或者,如果您已经在使用lodash(仅在这种情况下-不要导入lodash只是为了使用下面的解决方案),则可以使用maxBy
来实现。 maxBy
将大于number
的数字视为null
。
const floorPerDayMilestones = [25, 50, 75, 100, 125, 150, 175, 200]
const number = 136
let result = _.maxBy(floorPerDayMilestones, el=>el<=number?el:null);
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
答案 6 :(得分:1)
使用lodash,您可以使用_.sortedIndex
:
const numbers = [25, 50, 75, 100, 125, 150, 175, 200]
const closestNum = (arr, n) => {
let index = _.sortedIndex(arr, n)
return arr[index] == n ? arr[index] : arr[index-1]
}
console.log(closestNum(numbers, 135)) // 120
console.log(closestNum(numbers, 160)) // 150
console.log(closestNum(numbers, 180)) // 175
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
使用JS,您只需使用Array.reduceRight
:
const numbers = [25, 50, 75, 100, 125, 150, 175, 200]
const closestNum = (arr, n) => arr.reduceRight((r,c) => !r && c < n ? c : r, 0)
console.log(closestNum(numbers, 135)) // 120
console.log(closestNum(numbers, 160)) // 150
console.log(closestNum(numbers, 180)) // 175
由于它将从右侧开始,因此您所关心的就是找到小于参数n
的第一个数字。
您还可以执行Array.reverse
,然后执行Array.filter
(如果不想mutate
使用数组,请使用Array.from
):
const numbers = [25, 50, 75, 100, 125, 150, 175, 200]
const closestNum = (arr, n) => arr.reverse().find(x => x < n)
// To not mutate use `Array.from`
// const closestNum = (arr, n) => Array.from(arr).reverse().find(x => x < n)
console.log(closestNum(numbers, 135)) // 120
console.log(closestNum(numbers, 160)) // 150
console.log(closestNum(numbers, 180)) // 175