3

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.

marukobotto
  • 759
  • 3
  • 12
  • 26

7 Answers7

1

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 ) );
Community
  • 1
  • 1
Christos
  • 53,228
  • 8
  • 76
  • 108
  • @SagnikChakraborti You could think of this based on the above example. `Hint`: focus on `filter` function, what does this function? A detailed explanation of this mehtod can be found here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter. If you can't make it, just let me know. – Christos Oct 22 '16 at 07:50
  • For `number = 5`, it should return `7`, not `2`. – georg Oct 22 '16 at 08:20
  • @georg hm...you are pretty correct ! Thank you very much for spotting this. I will correct it immediately – Christos Oct 22 '16 at 08:21
1

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);
Mamdouh Saeed
  • 2,302
  • 1
  • 9
  • 11
1

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

less efficient, but more idiomatic:

let minNext = (a, el) => Math.min.apply(0, a.filter(x => x > el));
georg
  • 211,518
  • 52
  • 313
  • 390
0

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
youssouf
  • 381
  • 2
  • 11
0

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);
Redu
  • 25,060
  • 6
  • 56
  • 76
0

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]);
hakany
  • 7,134
  • 2
  • 19
  • 22
  • what if there are duplicates of the minimum value? – Redu Oct 22 '16 at 07:50
  • I have updated my reaction. I see in your post you will get the next large number of variable "RandomNumber". If that is not the case you can loop through the array until you find the next large number which is not equal to the first, in this case sorterMyArray[0]. – hakany Oct 22 '16 at 08:00
0

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));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • If my array is `var array = [{ user: 1, position: 2 }, { user: 2, position: 6 }, { user: 3, position: 4 }, {user:4, position: 5}]` and I search for `number = 5`, then it's returning `undefined` where it should have returned `6` – marukobotto Oct 22 '16 at 08:07
  • i get the wanted result, please see third example. – Nina Scholz Oct 22 '16 at 08:10