0

I have an array of objects, an example of an object is:

$scope.object = {
    Title: 'object1',
    Description: 'lorem ipsum',
    Value: 123
};

So my array would consist of several object with these attributes, how can I find the object with the highest 'value', in my array?

I have looked into other questions, like this, but they are about arrays with pure values in them, not objects.

What I'd really want is to find the object with the highest value inside my html, maybe using angular $filter, something like this:

ng-repeat="item in objectArray | filter: filterHere "

But it would also work with a function, right now I do it like this, but I don't like this solution at all:

var value = 0;
angular.forEach($scope.objectArray, function(object){
    if(object.Value > value){
        value = object.Value;
        $scope.itemToSeeInView = object;
    }
});

So if the value of the object is greater than the var value item to see in view is set to that object, if the $filter in the view won't work, could this function be improved?

Community
  • 1
  • 1
sch
  • 1,368
  • 3
  • 19
  • 35

4 Answers4

2

You can use the built in sort method

$scope.objectArray.sort(function(a,b) { return a.Value < b.Value})[0]
George Chen
  • 6,592
  • 5
  • 21
  • 23
  • This works, but it is not in the html (will look for other solutions, but very helpful, thank you) – sch Mar 15 '16 at 13:00
  • I got a problem testing this, seems like if the value is over 100 it can be wrong, it just said 42 was higher than 135.. something with INT or float or what do you think? – sch Mar 15 '16 at 13:14
  • is Value a string or number? – George Chen Mar 15 '16 at 13:32
  • Value is a number, but do it have to be float maybe? – sch Mar 16 '16 at 05:51
  • I was wrong, somehow, somewhere it got changed to a String, will find where and fix it, then it will work! Thank you – sch Mar 16 '16 at 06:34
2

If you want to display just the min/max from an array, you can use a orderby filter, and combine it with the limitTo filter. that would look something like:

<tr ng-repeat="p in vm.persons | orderBy:'someValue' |limitTo:1" >

or in case of your own example

ng-repeat="item in objectArray | orderBy:'Value': true | limitTo:1"

I created a small plnkr so you can see this in action. The plunk is an extended version of what I described above, and let's you toggle a few of the things. Drop me a note if i did put in too much. I will clean it up accordingly.

Sander Elias
  • 754
  • 6
  • 9
  • This works, and I will probably go with this solution, but! I get the same problem I did with the answer above, somehow it says that 42 is higher than 135, why could this be? – sch Mar 15 '16 at 13:23
  • Are you sure your values are numbers and not strings? it works as expected in my sample. – Sander Elias Mar 15 '16 at 13:24
  • On Second thought, this might have changed between angular versions. What version are you on? – Sander Elias Mar 15 '16 at 13:28
  • i am using the newest (1.5) – sch Mar 16 '16 at 05:51
  • The problem was that somewhere in my program 'Value' got changed to a string, don't know where or why, will find it and fix it, thanks! – sch Mar 16 '16 at 06:35
0

I am afraid if you can get optimized solution with $filter, but here is the quick solution.

$scope.friends = [
    {name:'John', age:25},
    {name:'Mary',  age:35},
    {name:'Mike', age:18},
    {name:'Adam', age:35},
    {name:'Julie', age:23}
 ];

var arr = [];
angular.forEach($scope.friends, function(item) {
  arr.push(item["age"]);
});
$scope.result = arr.sort().reverse()[0]; // Highest value.

jsfiddle Demo

Siraj Hussain
  • 874
  • 9
  • 25
0

When you are using ES6 (with a transpiler), you can use this code:

const friends = [
    {name:'John', age:25},
    {name:'Mary',  age:35},
    {name:'Mike', age:18},
    {name:'Adam', age:35},
    {name:'Julie', age:23}
];

const maxObject = friends.find(friend => !friends.some(item => item.age > friend.age));
str
  • 42,689
  • 17
  • 109
  • 127