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?

7
  • Do you want to solve this in the controller or in the template. Both is possible. Commented Mar 15, 2016 at 12:42
  • I would like it to be solved in the template, but it would be ok to solve in controller if needed Commented Mar 15, 2016 at 12:43
  • Ok, are the values always' positive? Commented Mar 15, 2016 at 12:45
  • yes they are (but can be null) Commented Mar 15, 2016 at 12:46
  • Easiest method is to create a simple custom filter, I'll whip you up an example. Commented Mar 15, 2016 at 12:48

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
Are you sure your values are numbers and not strings? it works as expected in my sample.
On Second thought, this might have changed between angular versions. What version are you on?
i am using the newest (1.5)
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!
2

You can use the built in sort method

$scope.objectArray.sort(function(a,b) { return a.Value < b.Value})[0]

5 Comments

This works, but it is not in the html (will look for other solutions, but very helpful, thank you)
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?
is Value a string or number?
Value is a number, but do it have to be float maybe?
I was wrong, somehow, somewhere it got changed to a String, will find where and fix it, then it will work! Thank you
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.