Find closest index of array using javascript - Stack Overflow

I have the following function:(function(){function closest (num, arr) {var curr = arr[0];var diff = Ma

I have the following function:

(function(){
    function closest (num, arr) {
        var curr = arr[0];
        var diff = Math.abs (num - curr);
        for (var val = 0; val < arr.length; val++) {
            var newdiff = Math.abs (num - arr[val]);
            if (newdiff < diff) {
                diff = newdiff;
                curr = arr[val];
            }
        }
        return curr;
    }
    var _array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
        _number = 155;

    return closest (_number, _array);
})()

Result is 162


In this array I want to show the closest index to the result! The result should be number 4

I have the following function:

(function(){
    function closest (num, arr) {
        var curr = arr[0];
        var diff = Math.abs (num - curr);
        for (var val = 0; val < arr.length; val++) {
            var newdiff = Math.abs (num - arr[val]);
            if (newdiff < diff) {
                diff = newdiff;
                curr = arr[val];
            }
        }
        return curr;
    }
    var _array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
        _number = 155;

    return closest (_number, _array);
})()

Result is 162


In this array I want to show the closest index to the result! The result should be number 4

Share Improve this question edited Oct 6, 2022 at 8:05 EugenSunic 13.7k15 gold badges66 silver badges95 bronze badges asked Feb 2, 2020 at 17:48 bakhshibakhshi 331 silver badge4 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

Here is a more straightforward solution (not optimal):

  1. find the difference between the number and the numbers in the array
  2. find the minimum in the new array of differences
  3. Now find the index of that minimum

var _array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
  _number = 155;

// closest index?

const diffArr = _array.map(x => Math.abs(_number - x));
const minNumber = Math.min(...diffArr);
const index = diffArr.findIndex(x => x === minNumber);

console.log('Result index', index);

You could store the index and return this value.

function closest(num, arr) {
    var curr = arr[0],
        diff = Math.abs(num - curr),
        index = 0;

    for (var val = 0; val < arr.length; val++) {
        let newdiff = Math.abs(num - arr[val]);
        if (newdiff < diff) {
            diff = newdiff;
            curr = arr[val];
            index = val;
        }
    }
    return index;
}

var array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
    number = 155;

console.log(closest(number, array));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745332786a4622961.html

相关推荐

  • Find closest index of array using javascript - Stack Overflow

    I have the following function:(function(){function closest (num, arr) {var curr = arr[0];var diff = Ma

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信