0

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

Sebastian
  • 21
  • 1
  • 5
  • 2
    What do you mean by *"highest x and lowest y*"? What is this object for the given input? – adiga Jun 17 '20 at 12:53
  • 1
    [Compare JavaScript Array of Objects to Get Min / Max](https://stackoverflow.com/questions/8864430) – adiga Jun 17 '20 at 12:54
  • 1
    What are you trying to achieve? Because your description sounds *close* to having a bunch of points and wanting to find their bounding box... – Niet the Dark Absol Jun 17 '20 at 12:56

1 Answers1

0

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)
Mechanic
  • 5,015
  • 4
  • 15
  • 38