Obtain Smallest Value from Array in JavaScript

Obtain smallest value from array in Javascript?

Jon Resig illustrated in this article how this could be achieved by extending the Array prototype and invoking the underlying Math.min method which unfortunately doesn't take an array but a variable number of arguments:

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

and then:

var minimum = Array.min(array);

Find smallest value in Array without Math.min

Now when look at my own question.. it looks so silly of me.

Below would be perfect solution to find the smallest number in an array:

By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts

var arr = [5, 1, 9, 5, 7];
var smallest = arr.sort((a, b) => a - b);
alert(smallest[0]);

Compare JavaScript Array of Objects to Get Min / Max

One way is to loop through all elements and compare it to the highest/lowest value.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity
var lowest = Number.POSITIVE_INFINITY;
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
tmp = myArray[i].Cost;
if (tmp < lowest) lowest = tmp;
if (tmp > highest) highest = tmp;
}
console.log(highest, lowest);

How to find the minimum in an array excluding 0?

Simply change your code to the following,

var smallest = 0;
var biggest = 0;
var merged = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ];

for (let i = 0; i < merged.length; i++) {

if (merged[i] > biggest && merged[i] != 0) {
biggest = merged[i];
}

if(smallest == 0 || (merged[i] < smallest && merged[i] != 0)) {
smallest = merged[i];
}

console.log('the biggest is', biggest, 'in the iteration', i)
console.log('the smallest is', smallest, 'in the iteration', i)
}

I assumed you no need to check the index of 0 valued item in the array.

Smallest number in array and its position

Just loop through the array and look for the lowest number:

var index = 0;
var value = temp[0];
for (var i = 1; i < temp.length; i++) {
if (temp[i] < value) {
value = temp[i];
index = i;
}
}

Now value contains the lowest value, and index contains the lowest index where there is such a value in the array.

How can I get the smallest two numbers from an array in js?

  • Sort the array in the ascending order.
  • Use Array#slice to get the first two elements (the smallest ones).

var arr = [5, 4, 7, 2, 10, 1],    res = arr.sort((a,b) => a - b).slice(0, 2);    console.log(res);

find smallest number in array javascript

Try Math.min(). It gets numbers and returns the minimum from them. I used the map() function to iterate over each nested array, then have used Math.min() with the ..., which will destroy the nested array and pass it to the function like Math.min(4,5,1,3) for the first nested one

ES6

var arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];

var minArr = arr.map(item => Math.min(...item));

console.log(minArr);

Get key with minimum value

You can get the key and value using Object.entries:

var arr = {  lst1: 300,  lst2: 381,  lst3: 4,  lst4: 4,  lst5: 49};
function lowestValueAndKey(obj) { var [lowestItems] = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2); return `Lowest value is ${lowestItems[1]}, with a key of ${lowestItems[0]}`;}
var lowest = lowestValueAndKey(arr);console.log(lowest);

How to get the index of the min value in an array using JavaScript?

You need to spread the array for getting the maximum. Otherwise you get NaN as value (via a stringed array) and this is not in the array (and not searchable).

A spreaded array takes all elements as parameter for the function (spread syntax ...).

In this case it follows this way

Math.max(...[11, 10, 10])

is evaluated as

Math.max(11, 10, 10)

function sayHello() {
arrayEdificiosNiveis = [11, 10, 10];
var indexMenor = arrayEdificiosNiveis.indexOf(Math.max(...arrayEdificiosNiveis));

console.log(indexMenor);
}

sayHello();


Related Topics



Leave a reply



Submit