Example 1: find biggest number in javascript array
var peopleData = [
{ name: "Paul", height: 180, age: 21 },
{ name: "Johnny", height: 198, age: 43 },
{ name: "Brad", height: 172, age: 49 },
{ name: "Dwayne", height: 166, age: 15 }
];
var maxHeight = 0;
for (var i = 0; i < heights.length; i++) {
if (peopleData[i].height > maxHeight) {
maxHeight = peopleData[i].height;
}
}
Example 2: kth largest element in an array javascript
function nthlargest(arra,highest){
var x = 0,
y = 0,
z = 0,
temp = 0,
tnum = arra.length,
flag = false,
result = false;
while(x < tnum){
y = x + 1;
if(y < tnum){
for(z = y; z < tnum; z++){
if(arra[x] < arra[z]){
temp = arra[z];
arra[z] = arra[x];
arra[x] = temp;
flag = true;
}else{
continue;
}
}
}
if(flag){
flag = false;
}else{
x++;
if(x === highest){
result = true;
}
}
if(result){
break;
}
}
return (arra[(highest - 1)]);
}
console.log(nthlargest([ 43, 56, 23, 89, 88, 90, 99, 652], 4));
Example 3: get largest no in the array javascript
var arr =[1,10,3]
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
var arr = [{a: 1},{a: 10},{a: 3}]
var values = arr.map(val => val.a);
var max = Math.max.apply(null, values);
console.log(max)
Example 4: function that search a biggest value in array javascript
function maisBaratosQue(valor, precos) {
return precos.filter(p => p <= valor);
}