当前位置: 动力学知识库 > 问答 > 编程问答 >

Find the lowest among the next highest elements in an array in JavaScript

问题描述:

The question seems to be a bit weird. Never mind.

This is an array

[2, 7, 5, 10]

If I want to get the next greater number after 2, here's my code

var MyArray = [2, 7, 5, 10];

var RandomNumber = 2;

var MinGreaterThanPos;

for (var i =0; i < MyArray.length; i++) {

if (MyArray[i] <= RandomNumber)

continue;

if (typeof(MinGreaterThanPos) == 'undefined' || MyArray[i] < MinGreaterThanPos)

{

MinGreaterThanPos = i;

}

}

alert(MyArray[MinGreaterThanPos]);

It will return 7.

What if I want to get the lowest among the greater numbers after 2?

That means, 7, 5, 10 are greater than 2. But I want to get 5, since the difference between 5 and 2 is lesser than any of the rest comparing with 2.

How will I do that?

Updated:

Coming to this point so far, what if there are objects inside an array?

For example:

var MyArray = [{user: 1, position:2}, {user:2, position: 6}, {user:3, position: 4}];

I want to do the same thing only with position. If I choose position 2, then the next position I am hoping to get back is 4 and not 6.

网友答案:

Another way to solve your problem is the following. Initially, we extend the Array adding a min method, in order we get the minimum element of an array. This is taken from here. Then we filter our array, in order we exlcude the enries that are less or equal to the number we hava as a threshold. Last we find the min number.

Array.min = function( array ){
    return Math.min.apply( Math, array );
};

var numbers = [2, 7, 5, 10];
var number = 5;
var numbers = numbers.filter( function( n ){
     return n > number;
});
console.log( Array.min( numbers ) );
网友答案:

first you sort the array then you get next of last item equal to RandomNumber if there is duplicates

var MyArray = [2,2,2, 10, 7, 5,5,7,5];//to test duplicates
var RandomNumber = 2;
var srt = MyArray.sort(function(a,b){return a-b});
var MinGreaterThanPos = srt[srt.lastIndexOf(RandomNumber)+1];


alert(MinGreaterThanPos);
网友答案:

This returns the minimal of array elements greater than el:

function minNext (a, el) {
  var min = Infinity;
  
  for (let x of a) {
    if (x > el && x - el < min - el)
      min = x;
  }
  
  return min;
}

//

let a = [1,9,2,8,3,-2,7,4,-3,6,5,5,5];
for (let x of a)
  console.log(x, minNext(a, x))
网友答案:

you can use this

      var ar = [2,7,5,10];
      Math.min.apply(undefined, ar.filter(function(x,y){return y > 0}));
      //for any explanation, tell it in comment
网友答案:

You might do like this in a single pass. It takes into account the duplicates as well;

var arr = [2, 7, 5, 2, 10],
 result = arr.reduce((p,c) => c < p[0] ? (p[0] = c,p)
                                       : c < p[1] ? (p[0] !== c && (p[1] = c),p)
                                                  : p, [Infinity,Infinity])[1];
console.log(result);
网友答案:

You can first sort and then loop through the array and until you find the next larger value. This way you will always have the second lowest value even if you have multiple.

var MyArray = [2,7,5,10];
var RandomNumber = 2;
var MinGreaterThanPos;

sortedMyArray = MyArray.sort(function(a, b){return a-b});
for(var i in sortedMyArray) {
    if (sortedMyArray[i] > RandomNumber) {
        MinGreaterThanPos = i;
        break;
    }
}
alert(sortedMyArray[MinGreaterThanPos]);

You can do the same for position as:

var MyArray = [{user: 1, position:2}, {user:2, position: 6}, {user:3, position: 4}];
var RandomNumber = 2;
var MinGreaterThanPos;

sortedMyArray = MyArray.sort(function(a, b){return a.position-b.position});
for(var i in sortedMyArray) {
    if (sortedMyArray[i].position > RandomNumber) {
        MinGreaterThanPos = i;
        break;
    }
};
alert(sortedMyArray[MinGreaterThanPos]);

And if your don't want to use RandomNumber

var MyArray = [{user: 1, position:2}, {user:2, position: 6}, {user:3, position: 4}];
var MinGreaterThanPos;

sortedMyArray = MyArray.sort(function(a, b){return a.position-b.position});
for(var i in sortedMyArray) {
    if (sortedMyArray[i].position > sortedMyArray[0].position) {
        MinGreaterThanPos = i;
        break;
    }
};
alert(sortedMyArray[MinGreaterThanPos]);
网友答案:

You could use Array#reduce.

function getItem(array, search) {
    return array.reduce(function (r, a) {
        return a.position > search && (!r || r.position > a.position) ? a : r;
    }, undefined);
}

var array1 = [{ user: 1, position: 2 }, { user: 2, position: 6 }, { user: 3, position: 4 }],
    array2 = [{ user: 1, position: 2 }, { user: 2, position: 6 }, { user: 3, position: 4 }, { user: 4, position: 5 }];

console.log(getItem(array1, 2));
console.log(getItem(array2, 2));
分享给朋友:
您可能感兴趣的文章:
随机阅读: