简体   繁体   English

在 object 数组中找到最低和最高

[英]find lowest and highest in array of object

I need to find 4 values in this order in a array of object:我需要在 object 数组中按此顺序查找 4 个值:

  • lowest x & y最低 x & y

  • highest x and lowest y最高 x 和最低 y

  • highest x & y最高 x & y

  • highest y & lowest x最高 y 最低 x

my object is like this:我的 object 是这样的:

   object: [
            {id: 1, x: 160, y: 160},
            {id: 2, x: 192, y: 160},
            {id: 3, x: 224, y: 160},
            {id: 4, x: 224, y: 192},
            {id: 5, x: 192, y: 192},
            {id: 6, x: 160, y: 192},
            {id: 7, x: 160, y: 224},
            {id: 8, x: 192, y: 224},
            {id: 9, x: 224, y: 224}
           ],

the result must be an array with those 4 object.结果必须是包含 4 个 object 的数组。

Thanks谢谢

This is not the complete answer;这不是完整的答案; highest x and lowest y and vice versa remained but it might give other contributors some insights how to solve the problem: highest x and lowest y仍然存在,反之亦然,但它可能会给其他贡献者一些如何解决问题的见解:

 const object = [ {id: 9, x: 224, y: 224}, {id: 2, x: 192, y: 160}, {id: 3, x: 224, y: 160}, {id: 4, x: 224, y: 192}, {id: 5, x: 192, y: 192}, {id: 6, x: 160, y: 192}, {id: 7, x: 160, y: 224}, {id: 8, x: 192, y: 224}, {id: 1, x: 160, y: 160}, {id: 9, x: 224, y: 224} ]; object.sort((objA, objB) => (objA.x * objA.y) - (objB.x * objB.y)); const HighestXAndHighestY = object[object.length-1]; const lowestXAndlowestY = object[0]; console.log(HighestXAndHighestY) console.log(lowestXAndlowestY)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM