简体   繁体   English

在JavaScript中找到数组中下一个最高元素中的最低元素

[英]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 如果我想在2之后得到下一个更大的数字,这是我的代码

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. 它将返回7。

What if I want to get the lowest among the greater numbers after 2? 如果我想要什么,以获得中最低的greater数字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. 这意味着, 7, 5, 10是大于2,但我想5,因为5和2之间的差大于任何其余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 . 我想只用position做同样的事情。 If I choose position 2, then the next position I am hoping to get back is 4 and not 6. 如果我选择位置2,那么我希望回到的下一个位置是4而不是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. 最初,我们扩展Array添加一个min方法,以便我们获得数组的最小元素。 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 . 然后我们过滤我们的数组,以便我们排除小于或等于我们作为阈值的数量的enries。 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 首先你对数组进行排序然后你得到的最后一项的下一项等于RandomNumber如果有重复项)

 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 ab}); var MinGreaterThanPos = srt[srt.lastIndexOf(RandomNumber)+1]; alert(MinGreaterThanPos); 

This returns the minimal of array elements greater than el : 这将返回大于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)) 

less efficient, but more idiomatic: 效率较低,但更惯用:

let minNext = (a, el) => Math.min.apply(0, a.filter(x => x > el));

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); 

As per objects as array items you simply the modify the code to show as follows; 根据对象作为数组项,您只需修改代码即可显示如下;

 var arr = [{user: 1, pos:2}, {user:2, pos: 6}, {user:3, pos: 4}, {user:4, pos: 12}, {user:5, pos: 9}], result = arr.reduce((p,c) => c.pos < p[0].pos ? (p[0] = c,p) : c.pos < p[1].pos ? (p[0].pos !== c.pos && (p[1] = c),p) : p, [{pos:Infinity},{pos: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 如果你不想使用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 . 你可以使用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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM