Finding the Max Value of an Attribute in an Array of Objects

Finding the max value of an attribute in an array of objects

To find the maximum y value of the objects in array:

    Math.max.apply(Math, array.map(function(o) { return o.y; }))

or in more modern JavaScript:

    Math.max(...array.map(o => o.y))

How to Find the max value of an attribute in an array of objects

As I am using Angular needed to add some types. Based on @Mehdi Taher answer I have added some typing and ended up like this -

const sortedHolidays = holidays.sort(
(a: IHoliday, b: IHoliday) => {
return a.holiday_on === b.holiday_on
? b.holiday_for - a.holiday_for
: 0;
}
);

let unique: { [key: string]: IHoliday | boolean } = {};
const filtered = duplicateHolidays.filter(
(holiday: IHoliday) => {
if (unique[holiday.holiday_on]) {
return false;
}
unique[holiday.holiday_on] = true;
return true;
}
);

Finding the max value of an attribute in an array of objects and return the entire object

If A is the array of objects.

function return_Max_Object(A)
{
var M = -Infinity;
var Max_index = -1;
for(var i =0;i<A.length;i++)
{
if(A[i].value>M)
{
M = A[i].value;
Max_index = i;
}
}
return A[Max_index];
}

Find maximum value of an attribute in a array of objects

I am afraid you'll have to do a linear search:

public static WaterBody mostPowerful(WaterBody[] waterBodies) {
double maxValue = -1;
int indexOfMaxValue = -1;
for(int i = 0; i < waterBodies.length; i++) {
if(waterBodies[i].getElectricPower() > maxValue) {
indexOfMaxValue = i;
}
}
return waterBodies[indexOfMaxValue];
}

A more elegant solution can be achieved by using a Comparator. It defines an Interface, which tells if one object is "bigger" or "smaller" than another. You can define a Comparator like that:

Comparator<String> cmp = new Comparator<WaterBody>() {
@Override
public int compare(WaterBody o1, WaterBody o2) {
return Double.compare(o1.getElectricPower(), o2.getElectricPower());
}
};

If you'll need do operations like these often, it is advised to have a collection which always keeps it's content sorted, like a TreeSet
It would be a lot smarter to use a collection, which keeps it's content sorted.

TreeSet<WaterBody> waterBodiesTree = new TreeSet<>(cmp);
Collections.addAll(waterBodiesTree, waterBodies);
WaterBody smallest = waterBodiesTree.first();
WaterBody biggest = waterBodiesTree.last();

If you only need to sort your array, the first example I used turns into a one-liner (using the same Comparator as defined earlier):

public static WaterBody mostPowerful(WaterBody[] waterBodies) {
return Collections.max(waterbodies, cmp);
}

Get max values from each array of objects

You could use the spread syntax and take the max of mapped value.

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables  (for destructuring assignment) are expected.

var data = [[{ a: "b", value: 12}, {a: "bb", value: 39 }, {a: "bb", value: 150 }], [{ a: "c", value: 15}, {a: "cc", value: 83 }, {a: "ccc", value: 12 }], [{ a: "d", value: 55}, {a: "dd", value: 9 }, {a: "dd", value: 1 }]],

result = data.map(a => Math.max(...a.map(b => b.value)));

console.log(result);

How can I find the max attribute in an array of objects?

All examples assume that $prop is the name of an object property like value in your example:

function max_attribute_in_array($array, $prop) {
return max(array_map(function($o) use($prop) {
return $o->$prop;
},
$array));
}
  • array_map takes each array element and returns the property of the object into a new array
  • Then just return the result of max on that array

For fun, here you can pass in max or min or whatever operates on an array as the third parameter:

function calc_attribute_in_array($array, $prop, $func) {
$result = array_map(function($o) use($prop) {
return $o->$prop;
},
$array);

if(function_exists($func)) {
return $func($result);
}
return false;
}

$max = calc_attribute_in_array($data_points, 'value', 'max');
$min = calc_attribute_in_array($data_points, 'value', 'min');

If using PHP >= 7 then array_column works on objects:

function max_attribute_in_array($array, $prop) {
return max(array_column($array, $prop));
}

Here is Mark Baker's array_reduce from the comments:

$result = array_reduce(function($carry, $o) use($prop) {
$carry = max($carry, $o->$prop);
return $carry;
}, $array, -PHP_INT_MAX);


Related Topics



Leave a reply



Submit