0

Basically what I'm trying to achieve, is the lowest integer from an object thats in an array with other objects.

r.Collection

is an array that holds 3 objects, so it looks something like this:

[{foo:"bar",abc:"xyz",number:1},{foo:"car",abc:"hcu",number:2},{foo:"tar",abc:"uif",number:3}]

Now the goal, is to sort through this array. and look at each of the number values, once it finds the lowest value. I need it to pull that entire object out of the array to use later.

Currently what i'm trying to do isn't working and i'm not sure. I could be doing this totally wrong

 for (o in r.Collection) {
                    var data = r.Collection[o]
                    console.log(Math.min(data.number))
                }

I know to use Math.min to find the smallest number in an array, but when I run this code, every object gets printed.

How do I sort through this array, to pull our the object with the lowest number value?

0

3 Answers 3

4

Math.min is a little to simple for this — there's no way to specify what you mean by min in an object with several values.

You can use reduce() to loop over the items in the list and keep track of the current lowest. The result will be the lowest in the list based on the function passed to reduce:

let l = [{foo:"bar",abc:"xyz",number:1},{foo:"car",abc:"hcu",number:2},{foo:"tar",abc:"uif",number:3}]

let lowest = l.reduce((lowest, item) => item.number < lowest.number ? item: lowest)
console.log(lowest)

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

Comments

0

Math.min() can work, but it would require that you use destructuring and some massaging of the array index to account for 0-base.

var r = {};
r.collection = [{foo:"bar",abc:"xyz",number:1},{foo:"car",abc:"hcu",number:2},{foo:"tar",abc:"uif",number:3}];
var min = r.collection[Math.min(...(r.collection.map(function(item){return item.number})))-1];

console.log(min);

3 Comments

Does this require the array to already be sorted? Unless I'm misunderstanding something this seems to fail when array is not already sorted by number.
@MarkMeyer Yes, it does. This is why I say it can work, but it's not the best approach.
Actually it seems to require that number corresponds to the index in the array. For example, given [{foo:"bar",abc:"xyz",number:2},{foo:"car",abc:"hcu",number:5}] it returns the item number 5.
0

You could also simply sort the array via Array.sort and pick the first item from it:

let data = [{foo:"car",abc:"hcu",number:2},{foo:"bar",abc:"xyz",number:1},{foo:"tar",abc:"uif",number:3}]

let result = data.sort((a,b) => a.number - b.number)[0]

console.log(result)

If you need the max simply reverse the sort by b.number - a.number etc.

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.