简体   繁体   中英

find maximum number in array of objects?

how I can find the bigger and smaller number in the most efficient way in javascript (or the most easiest way)

something like this:

function findMaxFromArray() {
  // logic
  return { x: ..., y: ... }
}

I tried Math.max(array.x) and is not working

here the array:

 let array = [{ "x": 0, "y": 0 }, { "x": 3.6417243968365076, "y": 10 }, { "x": 9.34437089784549, "y": 20 }, { "x": 0.7464057532722523, "y": 30 }, { "x": 10.254345841653576, "y": 40 }, { "x": 40.665266471734526, "y": 50 }, { "x": 13.605184580988041, "y": 60 }, { "x": 56.1970151503722, "y": 70 }, { "x": 46.30684270171864, "y": 80 }, { "x": 86.97417514998948, "y": 90 }, { "x": 19.086639005143002, "y": 100 }, { "x": 74.53073691009028, "y": 110 }, { "x": 80.51273650018987, "y": 120 }, { "x": 125.67651066602721, "y": 130 }, { "x": 94.92603682139705, "y": 140 }, { "x": 81.41258620429245, "y": 150 }, { "x": 128.79236053348222, "y": 160 }, { "x": 101.73294154449238, "y": 170 }, { "x": 176.92343490767377, "y": 180 }, { "x": 148.01122343809243, "y": 190 }, { "x": 13.666705559256798, "y": 200 }, { "x": 190.95447678085023, "y": 210 }, { "x": 186.02329838354444, "y": 220 }, { "x": 207.04643642736238, "y": 230 }, { "x": 75.3481702154557, "y": 240 }, { "x": 49.14228670472814, "y": 250 }, { "x": 203.65260758421869, "y": 260 }, { "x": 69.53501551989147, "y": 270 }, { "x": 269.52487694376975, "y": 280 }, { "x": 184.31351948105274, "y": 290 }, { "x": 19.72787138830958, "y": 300 } ]

result output:

basically, we give an array of objects with x , y as a parameter to a function.

and the function should return the maximum x and maximum y

(for now, I don't need to calculate the minimum since is always 0, but it will help as well as a solution in that topic too)

 // output sample return { x: 269.52487694376975, y: 300 } // or like this if it return also minimum return { max: { x: 269.52487694376975, y: 300 }, min: { x: 0 y: 0 } }

I have tried before, but with for loops, and isn't giving me any result at all. and a lot of code. (if possible a simple way)

thanks

Use Array.prototype.map and Math.max

 let array = [{ "x": 0, "y": 0 }, { "x": 3.6417243968365076, "y": 10 }, { "x": 9.34437089784549, "y": 20 }, { "x": 0.7464057532722523, "y": 30 }, { "x": 10.254345841653576, "y": 40 }, { "x": 40.665266471734526, "y": 50 }, { "x": 13.605184580988041, "y": 60 }, { "x": 56.1970151503722, "y": 70 }, { "x": 46.30684270171864, "y": 80 }, { "x": 86.97417514998948, "y": 90 }, { "x": 19.086639005143002, "y": 100 }, { "x": 74.53073691009028, "y": 110 }, { "x": 80.51273650018987, "y": 120 }, { "x": 125.67651066602721, "y": 130 }, { "x": 94.92603682139705, "y": 140 }, { "x": 81.41258620429245, "y": 150 }, { "x": 128.79236053348222, "y": 160 }, { "x": 101.73294154449238, "y": 170 }, { "x": 176.92343490767377, "y": 180 }, { "x": 148.01122343809243, "y": 190 }, { "x": 13.666705559256798, "y": 200 }, { "x": 190.95447678085023, "y": 210 }, { "x": 186.02329838354444, "y": 220 }, { "x": 207.04643642736238, "y": 230 }, { "x": 75.3481702154557, "y": 240 }, { "x": 49.14228670472814, "y": 250 }, { "x": 203.65260758421869, "y": 260 }, { "x": 69.53501551989147, "y": 270 }, { "x": 269.52487694376975, "y": 280 }, { "x": 184.31351948105274, "y": 290 }, { "x": 19.72787138830958, "y": 300 } ] const max = { x: Math.max(...array.map(e => ex)), y: Math.max(...array.map(e => ey)), } console.log(max)

 let array = [{ "x": 0, "y": 0 }, { "x": 3.6417243968365076, "y": 10 }, { "x": 9.34437089784549, "y": 20 }, { "x": 0.7464057532722523, "y": 30 }, { "x": 10.254345841653576, "y": 40 }, { "x": 40.665266471734526, "y": 50 }, { "x": 13.605184580988041, "y": 60 }, { "x": 56.1970151503722, "y": 70 }, { "x": 46.30684270171864, "y": 80 }, { "x": 86.97417514998948, "y": 90 }, { "x": 19.086639005143002, "y": 100 }, { "x": 74.53073691009028, "y": 110 }, { "x": 80.51273650018987, "y": 120 }, { "x": 125.67651066602721, "y": 130 }, { "x": 94.92603682139705, "y": 140 }, { "x": 81.41258620429245, "y": 150 }, { "x": 128.79236053348222, "y": 160 }, { "x": 101.73294154449238, "y": 170 }, { "x": 176.92343490767377, "y": 180 }, { "x": 148.01122343809243, "y": 190 }, { "x": 13.666705559256798, "y": 200 }, { "x": 190.95447678085023, "y": 210 }, { "x": 186.02329838354444, "y": 220 }, { "x": 207.04643642736238, "y": 230 }, { "x": 75.3481702154557, "y": 240 }, { "x": 49.14228670472814, "y": 250 }, { "x": 203.65260758421869, "y": 260 }, { "x": 69.53501551989147, "y": 270 }, { "x": 269.52487694376975, "y": 280 }, { "x": 184.31351948105274, "y": 290 }, { "x": 19.72787138830958, "y": 300 } ] console.log(Math.max(...array.map(elem => elem.x))) console.log(Math.max(...array.map(elem => elem.y))) console.log({ x:Math.max(...array.map(elem => elem.x)), y:Math.max(...array.map(elem => elem.y)) })

This function returns max number and min from array

function sortThem(numbers) {

  let minNum = numbers[0]
  let maxNum = numbers[0]

  for (let i = 0; i < numbers.length; i++) {
    if (minNum > numbers[i]) {
      minNum = numbers[i]
    } else if (maxNum < numbers[i]) {
      maxNum = numbers[i]
    }
  }

  const minMax = [minNum, maxNum]
  return minMax

}

Then use it like this

const results = sortThem([2, 9, 10, 17, 45])

console.log(results)

You can use Array.prototype.reduce() and calculate everything in one pass/iteration.

const array = [
  {
    x: 0,
    y: 0,
  },
  {
    x: 3.6417243968365076,
    y: 10,
  },
  {
    x: 9.34437089784549,
    y: 20,
  },
];

const findMaxAndMin = data => {
  return data.reduce(
    (acc, cur) => {
      return {
        max: {
          x: Math.max(cur.x, acc.max.x),
          y: Math.max(cur.y, acc.max.y),
        },
        min: {
          x: Math.min(cur.x, acc.min.x),
          y: Math.min(cur.y, acc.min.y),
        },
      };
    },
    { max: { x: null, y: null }, min: { x: null, y: null } }
  );
};

console.log(findMaxAndMin(array));

Here is an answer using Array.reduce.

// reduce loops on the array items and apply a callback function
const max = array.reduce(
  /**
   * callback function that is called for each element of the array from index 0 to index array.length-1
   * @param currentMax an object {x: number, y: number} containing the current maximum of x and y. It is initialized to the second argument of reduce
   * @param currentItem the current element of the loop
   * @returns the value of currentMax that will be used for the next step of the loop
   */
  (currentMax, currentItem) => {
    return {x: Math.max(currentMax.x, currentItem.x), y: Math.max(currentMax.y, currentItem.y)};
  }, array[0] /* initial maximum set to the first element of the array */);
console.log(max);

Hey you can use the underlying snippet to get your desired result:

var NewReturnObject = {
        min: {
            x: Math.min(...array.map(o => o.x)),
            y: Math.min(...array.map(o => o.y))
        },
        max: {
            x: Math.max(...array.map(o => o.x)),
            y: Math.max(...array.map(o => o.y))
        }
    }

    console.log(NewReturnObject);

For further references you can refer to underlying links as well: Obtain smallest value from array in Javascript and Finding the max value of an attribute in an array of objects

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.

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