1

I have a data showing the various data points on the chart.

var data = [{name:"s", y:2},{name:"e",y:90},{name:"tt",y:9},{name:"se",y:10}]

now i know with simple array you can use the Math.max....and Math.min to calculate the min/max values, not sure how can i achieve the same with an array of objects?

Thanks!

2 Answers 2

1

You could use .map to get right min and max values.

var data = [{name:"s", y:2},{name:"e",y:90},{name:"tt",y:9},{name:"se",y:10}];


var values = data.map(function( obj ) {
    return obj.y;
}); 

var min = Math.min.apply(null, values);
var max = Math.max.apply(null, values);

If you are using ES6, then you can get rid of .apply and replace it with spread operator.

var min = Math.min(...values);
var max = Math.max(...values);
Sign up to request clarification or add additional context in comments.

2 Comments

You can't pass an array to Math.max. You need to use apply.
@fubar Fixed it.
0

You can still use Math.max and Math.min. Just map the data first.

const max = Math.max.apply(null, data.map((point) => point.y));
const min = Math.min.apply(null, data.map((point) => point.y));

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.