如果不存在,从Array获取下一个最接近(更高)的值

时间:2018-01-04 04:34:47

标签: javascript arrays

else

我正在修改此代码以从数组中获取下一个最接近的值。如果值在数组中,那么它将返回该值。如果它不在数组中那么它应该返回最接近的更高值。 例如:[110,111,120,140,​​148,149,155,177,188,190]如果我搜索150,我应该得到155而不是149.我尝试但很难找到解决这个问题的方法。能否请你帮忙。感谢已修改

5 个答案:

答案 0 :(得分:1)

您正在启动for循环,并将i设置为1,这意味着您要从数组中的第二个元素开始。但是,您的比较standardArray[i] > targetVal忽略了standardArray[i-1]prevs)可能小于0的事实。

我用新的起始索引和变量名更新你的例子:

function getClosestValue(standardArray, targetVal) {
standardArray = standardArray.sort(function(a, b){return a-b});
        if (!(standardArray) || standardArray.length == 0) {
            return null;}
        if (standardArray.length == 1){
            return standardArray[0];}

        for (var i=0; i<standardArray.length-1; i++) {
            if (standardArray[i] >= targetVal) {
                var curr = standardArray[i];
                var next = standardArray[i+1]
                return Math.abs( curr-targetVal ) < Math.abs( next-targetVal ) ? curr : next;
            }
        }
        return standardArray[standardArray.length-1];        
    }

var temp = getClosestValue([110,111,188,190,177,120,140,120,149,148,155], 149)
console.log(temp);

答案 1 :(得分:0)

此解决方案应该有效:

&#13;
&#13;
function getClosestValue(input, array){
  var tempArray = array;
  var index = tempArray.sort().findIndex((item) => {
    return input < item;
  });
  if(index >= 0) {
    return array[index]; 
  } else {
    return null; // no answer
  }
}

var ary = [110, 111, 120, 140, 148, 149, 155, 177, 188, 190];

console.log(getClosestValue(150,ary));
&#13;
&#13;
&#13;

首先对数组进行排序。 (可能是未分类的)。然后,您找到第一个值大于输入值的项目。然后返回该索引的值。

答案 2 :(得分:0)

在我看来,在对数组进行排序后,您只需要第一个等于或高于 targetVal 的值,或者数组中的最高元素。

排序会影响数组,最好复制,然后排序,然后find满足“等于或大于”标准的第一个值,或者如果不满足,则返回最高值(最后在排序数组中)例如:

function getNextHighest(arr, val) {
  return arr.slice().sort().find((x,i,a) => i < a.length-1? x >= val : x);
}

var data = [110,111,188,190,177,120,140,120,149,148,155];
    
console.log(getNextHighest(data, 149));
console.log(getNextHighest(data, 120));
console.log(getNextHighest(data, 1000));
    

在原始代码中:

function getClosestValue(standardArray, targetVal) {

  // This assumes that standardArray is an array
  standardArray = standardArray.sort(function(a, b){return a-b});

  // Then there is a test for it. The test should be first.
  if (!(standardArray) || standardArray.length == 0) {
    return null;
  }

  if (standardArray.length == 1){
        return standardArray[0];
  }

  // Why start at 1?
  for (var i=1; i<standardArray.length; i++) {

    if (standardArray[i] > targetVal) {
      var prevs = standardArray[i-1];
      var currnt = standardArray[i];

      // This logic is unnecessary since the array is sorted, the first
      // higher value is the one required.
      // Also, the logic doesn't seem appropriate for what you want as it will
      // return the previous (lower) value if it's closer than the higher one
      return Math.abs(prevs - targetVal) < Math.abs(currnt - targetVal) ? prev : currnt;

      // So instead just return the higher value, replace the above with
      // return standardArray[i];
    }
  }

  // Otherwise it returns the last element in the array or undefined
  return standardArray[standardArray.length-1];        
}

答案 3 :(得分:0)

您也可以试试这个。

function getClosestValue(array, targetValue){
  if(array.indexOf(targetValue) != -1){ 
       return targetValue;
  }
  else{
    var tempValue;
    for(var i=0; 1 < array.length; i++){
        array = array.sort((a, b) => a - b);
        if(array[i] > targetValue){
           return array[i];
        }
    }
  }
}
var value = getClosestValue([110,111,188,190,177,120,140,120,148,155], 149);
console.log(value);

答案 4 :(得分:0)

在上面给出的答案中需要一点修改以获得下一个最接近的值。

首先按数字排序数组

// numberAs function will sort numeric array in asscending order
function numberAs(a,b) {
  return a-b;
}

function getClosestValue(input, array){
  // When you sort your array as numerically then you will able to get exact closest position of next higher value.
 // If you don't sort your array numerically then you will get 110 instead of getting 90 when search value 1 in array 
  var index = array.sort(numberAs).findIndex((item) => {
  return input < item;
   });
  if(index > 0) {
    return array[index]; 
  } else {
    return null; // no answer
  }
}

var ary = [110,90, 111, 1, 170, 180, 120, 140, 148, 149, 177, 155, 188, 190];

console.log(getClosestValue(1,ary));