0

Please find the below array JSON Object.

points:[{x:1, YValues:[34,45,56]}, {x:5, YValues:[20,12,30]}, {x:8, YValues:[12,13]}]

I want to find the maximum value of X and find the maximum value of YValues separately.

I don't expect for loop to find the maximum value. Expecting in simple way to find the maximum of X as well as maximum of YValues from points JSON object.

Is it possible to use Math.max or any custom function ?

Thanks, Siva

1
  • what you have tried ? Commented Jun 8, 2013 at 6:51

2 Answers 2

2

Something like this?

Math.max.apply(0,points.map(function(v){return v.x}));

Still a loop, but it's succinct.

Here's how to do it for YValues. A long line though:

Math.max.apply(0,[].concat.apply([],arr.map(function(v){return v.YValues})));
Sign up to request clarification or add additional context in comments.

9 Comments

whether it will return only maximum of X. how can i find maximum of YValues
See my edit. It becomes a bit unreadable tho, you might want to create separate variables or a little function.
thanks but i want only maximum of YValues[1] and X ? not whole YValues array.select all YValues[1] and find maximum and minmum as well
Math.max.apply(0,points.map(function(v){return v.x})); -> it returns Nan
sorry elclanrs. working fine. excellent solution. thank you very much
|
1

I made soultion using javascript 1.8 Array reduce method . Please note it works only in modern browsers

var max = yourObj.points.reduce( function ( a, b ){
    a.x = Math.max.apply( 0, [a.x,b.x] ) ;
    a.y = Math.max.apply( 0, [].concat( [ a.y ], b.YValues ) )
    return a;
}, { x :0, y :0 } );

the max variable contains maximum x and y

console.log( max.x );
console.log( max.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.