I need to find 4 values in this order in a array of object:
lowest x & y
highest x and lowest y
highest x & y
highest y & lowest x
my object is like this:
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.
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:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.