简体   繁体   English

如何使用JavaScript获取嵌套数组中特定值的最大值和最小值?

[英]How to take the max and min values of specific values in a nested array using JavaScript?

I want to take the maximum and the minimum of specific values in a nested array. 我想在嵌套数组中采用特定值的最大值和最小值。

An example would be a nested array being in the format [latitude, longitude]. 例如,嵌套数组的格式为[纬度,经度]。

The nested array I want to input is [[40, 50], [50, 60], [60, 70]]. 我要输入的嵌套数组是[[40,50],[50,60],[60,70]]。

Max longitude should output 70 and min longitude should output 50. 最大经度应输出70,最小经度应输出50。

Max latitude should output 60 and min latitude should output 40. 最大纬度应输出60,最小纬度应输出40。

Average longitude should output 60 and average latitude should output 50. 平均经度应输出60,平均纬度应输出50。

Now my question is how do I take the max and min values of longitude and latitude values in the nested array and find the average separately using JavaScript? 现在,我的问题是如何在嵌套数组中获取经度和纬度值的最大值和最小值,并使用JavaScript分别找到平均值?

What about something like this: 像这样的事情呢:

var numbers = [[1, 5], [2, 3], [4,10]];
// max for o[0];
Math.max.apply(Math, numbers.map(function(o) { return o[0]; }));
4
// max for o[1]
Math.max.apply(Math, numbers.map(function(o) { return o[1]; }));
10
// min for o[0]
Math.min.apply(Math, numbers.map(function(o) { return o[0]; }));
1
// min for o[1]
Math.min.apply(Math, numbers.map(function(o) { return o[1]; }));
3

Hope it helps 希望能帮助到你

 const latlon = [[40, 50], [50, 60], [60, 70]] const lat = latlon.map(a => a[0]) const lon = latlon.map(a => a[1]) const maxLat = Math.max(...lat) const minLat = Math.min(...lat) const avgLat = (lat.reduce((acc,cur) => acc + cur, 0))/lat.length console.log(maxLat, minLat, avgLat) const maxLon = Math.max(...lon) const minLon = Math.min(...lon) const avgLon = (lon.reduce((acc,cur) => acc + cur, 0))/lon.length console.log(maxLon, minLon, avgLon) 

Using the Array#reduce method, you could achieve this in a "functional" way. 使用Array#reduce方法,您可以通过“功能性”方式实现此目的。

Each method begins the reduction with an initial value taken from the input array, and then iterates through the array searching for and updating the current minimum or maximum in the array as it goes. 每种方法都以从输入数组中获取的初始值开始减少操作,然后迭代遍历数组以查找和更新数组中当前的最小或最大值。

Following with this approach, the "average" methods are implemented by composing both the "minimum" and "maximum" methods, and averaging the numeric result that they return: 遵循这种方法,通过组合“ minimum”和“ maximum”方法,并对它们返回的数值结果求平均值,来实现“ average”方法:

 var array = [[40, 50], [50, 60], [60, 70]] const findMinLatitude = (arr) => { return arr.reduce((min, item) => { return Math.min(min, item[0]) }, arr[0][0]) } const findMaxLatitude = (arr) => { return arr.reduce((max, item) => { return Math.max(max, item[0]) }, arr[0][0]) } const findAvgLatitide = (arry) => { return (findMinLatitude(arry) + findMaxLatitude(arry)) * 0.5 } const findMinLongitude = (arr) => { return arr.reduce((min, item) => { return Math.min(min, item[1]) }, arr[0][1]) } const findMaxLongitude = (arr) => { return arr.reduce((max, item) => { return Math.max(max, item[1]) }, arr[0][1]) } const findAvgLongitude = (arry) => { return (findMinLongitude(arry) + findMaxLongitude(arry)) * 0.5 } console.log('Min latitude', findMinLatitude(array)) console.log('Max latitude', findMaxLatitude(array)) console.log('Average latitude', findAvgLatitide(array)) console.log('Min longitude', findMinLongitude(array)) console.log('Max longitude', findMaxLongitude(array)) console.log('Average longitude', findAvgLongitude(array)) 

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

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