Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    get max,min y value for each x in javascript 2d integer array

    Hi all,

    this is my first post to this great useful forum.

    I have a javascript 2d array [x,y] that contains integer values like this:

    [0,0], [0,1], [0,2], [0,3],
    [1,0], [1,1], [1,2],
    [2,0], [2,1], [2,2],
    [0,-1], [0,-2], [0,-3],
    [1,-1], [1,-2],
    [2,-3]

    my question is how to get the max,min y value for each x.
    for the sample above it should give me another array with the following results
    [0,3],[0,-3],[1,2],[1,-2],[2,2],[2,-3]

    your help is highly appreciated

    Regards
    Omran

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,846
    Thanks
    21
    Thanked 157 Times in 148 Posts
    Run that in Firefox because toSource() is only supported in Gecko browsers:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
    	<title>TITLE</title>
    </head>
    <body>
    
    <script type="text/javascript">
    
    var arr = [[0,0], [0,1], [0,2], [0,3], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2], [0,-1], [0,-2], [0,-3], [1,-1], [1,-2], [2,-3]];
    var ordered = [];
    for (var i=0; i<arr.length; i++)
    {
    	ordered[arr[i][0]] = (ordered[arr[i][0]] == undefined) ? [] : ordered[arr[i][0]];
    	ordered[arr[i][0]].push(arr[i]);
    }
    
    var outputArr = [];
    var count = -1;
    for (var i=0; i<ordered.length; i++)
    {
    	var lowHighFinal = [];
    	for (var j=0; j<ordered[i].length; j++)
    	{
    		var yVal = ordered[i][j][1];
    		if (j > 0)
    		{
    			lowHighFinal[0] = (compareXY(yVal, lowHighFinal[0]) == -1) ? yVal : lowHighFinal[0];
    			lowHighFinal[1] = (compareXY(yVal, lowHighFinal[1]) == 1) ? yVal : lowHighFinal[1];
    		}
    		else
    		{
    			lowHighFinal = [yVal, yVal];
    		}
    	}
    	outputArr[++count] = [i, lowHighFinal[1]];
    	outputArr[++count] = [i, lowHighFinal[0]];
    }
    
    alert(outputArr.toSource());
    
    function compareXY(a, b)
    {
    	return (a >= b) ? 1 : -1;
    }
    
    </script>
    
    </body>
    </html>
    The code can probably be refactored a little bit, but this gives you an idea of how to do what you want.
    Last edited by chump2877; 05-19-2012 at 04:28 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank u chump2877 for your help

    the code works perfectly

    Regards
    Omran


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •