Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Finding lowest value of an array of objects

I am making a game with obstacles currently and I want to find the object with the lowest X value. Here is an example of an array.

var array = [{x:500, y:400}, {x:80, y:400}, {x:900, y:400}];

I want to be able to determine what is the lowest X in this group(80) and return it to me. Is there any way to do this?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You don't need to sort the array or use an external library. You can use Array.prototype.reduce to find the object with minimum x in linear time:

const array = [{x:500, y:400}, {x:80, y:400}, {x:900, y:400}];
const minX = array.reduce((acc, curr) => curr.x < acc.x ? curr : acc, array[0] || undefined);
console.log(minX)

about 4 years ago · Juan Pablo Isaza Report

0

You can use lodash

var array = [{
  x: 500,
  y: 400
}, {
  x: 80,
  y: 400
}, {
  x: 900,
  y: 400
}];
console.log(_.minBy(array, 'x'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

let min = Infinity
for (var i=0; i<array.length; i++){
    if (array[i].x<min) min = array[i].x
}

Here min is your answer. There might be easier way to do this, but his gets the job done.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!