javascript - Find min, max in array with objects - Stack Overflow

I have this array with objects inside:var pts = [{ "X": 52.67528921580262,"Y": 8.

I have this array with objects inside:

var pts = [
    { "X": 52.67528921580262,  "Y": 8.373513221740723 },
    { "X": 52.6759657545252,   "Y": 8.374114036560059 },
    { "X": 52.682574466310314, "Y": 8.37256908416748  },
    { "X": 52.68356308524067,  "Y": 8.373942375183105 },
    { "X": 52.68293869694087,  "Y": 8.375487327575684 },
    { "X": 52.67685044320001,  "Y": 8.376259803771973 },
    { "X": 52.6756535071859,   "Y": 8.379607200622559 },
    { "X": 52.676017795531436, "Y": 8.382096290588379 },
    { "X": 52.68101344348877,  "Y": 8.380722999572754 },
    { "X": 52.68351105322329,  "Y": 8.383641242980957 },
    { "X": 52.68,              "Y": 8.389             }
];

How I can find min,max for X and min,max for Y ?

I have this array with objects inside:

var pts = [
    { "X": 52.67528921580262,  "Y": 8.373513221740723 },
    { "X": 52.6759657545252,   "Y": 8.374114036560059 },
    { "X": 52.682574466310314, "Y": 8.37256908416748  },
    { "X": 52.68356308524067,  "Y": 8.373942375183105 },
    { "X": 52.68293869694087,  "Y": 8.375487327575684 },
    { "X": 52.67685044320001,  "Y": 8.376259803771973 },
    { "X": 52.6756535071859,   "Y": 8.379607200622559 },
    { "X": 52.676017795531436, "Y": 8.382096290588379 },
    { "X": 52.68101344348877,  "Y": 8.380722999572754 },
    { "X": 52.68351105322329,  "Y": 8.383641242980957 },
    { "X": 52.68,              "Y": 8.389             }
];

How I can find min,max for X and min,max for Y ?

Share Improve this question edited Jan 16, 2015 at 18:23 Sampson 269k76 gold badges545 silver badges568 bronze badges asked Jan 16, 2015 at 18:08 dert detgdert detg 3032 gold badges7 silver badges18 bronze badges 2
  • 1 Have you tried looping over the array, and paring values with four variables for {x/y} {min/max}? If you have tried that, what part of this approach gives you problems? – apsillers Commented Jan 16, 2015 at 18:11
  • Store the first objects x and y values as the min and max for x and y. Then loop through the rest of the objects. For each object, pare that objects x and y with the min and max for x and y. If you found a new min or max for x or y, replace the old one with it. – forgivenson Commented Jan 16, 2015 at 18:12
Add a ment  | 

3 Answers 3

Reset to default 6

You could call Math.min and Math.max, passing in a mapped array containing only the relevant values like this:

function endProp( mathFunc, array, property ) {
    return Math[ mathFunc ].apply(array, array.map(function ( item ) {
        return item[ property ];
    }));
}

var maxY = endProp( "max", pts, "Y" ), // 8.389
    minY = endProp( "min", pts, "Y" ); // 8.37256908416748

It would probably be easiest to sort the array.

array.sort(function minXToMaxX(a,b){
   return a.x - b.x;
});

Lowest x value is at array[0]. Highest x value is at array[array.length - 1];

This code works fine. See snippt for demo.

var pts = [{
  "X": 52.67528921580262,
  "Y": 8.373513221740723
}, {
  "X": 52.6759657545252,
  "Y": 8.374114036560059
}, {
  "X": 52.682574466310314,
  "Y": 8.37256908416748
}, {
  "X": 52.68356308524067,
  "Y": 8.373942375183105
}, {
  "X": 52.68293869694087,
  "Y": 8.375487327575684
}, {
  "X": 52.67685044320001,
  "Y": 8.376259803771973
}, {
  "X": 52.6756535071859,
  "Y": 8.379607200622559
}, {
  "X": 52.676017795531436,
  "Y": 8.382096290588379
}, {
  "X": 52.68101344348877,
  "Y": 8.380722999572754
}, {
  "X": 52.68351105322329,
  "Y": 8.383641242980957
}, {
  "X": 52.68,
  "Y": 8.389
}];
var xobj = [];
var yobj = [];
var len = pts.length;

for (var i = 0; i < len; i++) {

  xobj.push(parseFloat(pts[i].X));
  yobj.push(parseFloat(pts[i].Y));
}

//height x value
var value = xobj[0];
for (var n = 1; n < xobj.length; n++) {
  if (xobj[n] > value) {
    value = xobj[n];
  }
}

//lowest x value
var valuel = xobj[0];
for (var n = 1; n < xobj.length; n++) {
  if (xobj[n] < valuel) {
    valuel = xobj[n];
  }
}

//height y value
var valueY = yobj[0];
for (var n = 1; n < yobj.length; n++) {
  if (yobj[n] > valueY) {
    valueY = yobj[n];
  }
}

//lowest x value
var valuelY = yobj[0];
for (var n = 1; n < yobj.length; n++) {
  if (yobj[n] < valuelY) {
    valuelY = yobj[n];
  }
}

document.getElementById("demo").innerHTML = valuel

document.getElementById("demo1").innerHTML = value

document.getElementById("demo2").innerHTML = valuelY

document.getElementById("demo3").innerHTML = valueY
Minx:
<p id="demo"></p>
MaxX:
<p id="demo1"></p>
Miny:
<p id="demo2"></p>
MaxY:
<p id="demo3"></p>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745399103a4626018.html

相关推荐

  • javascript - Find min, max in array with objects - Stack Overflow

    I have this array with objects inside:var pts = [{ "X": 52.67528921580262,"Y": 8.

    8小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信