简体   繁体   English

从字符串数组中获取最大值/最小值(javascript)

[英]Get max / min from array of strings (javascript)

Any ideas to calculate min / max from array of strings?从字符串数组计算最小值/最大值的任何想法?

var arr = ['aab','aac','aad','abx'];

So far i have considered to use .sort() function depending max / min, and get first element of result.到目前为止,我已经考虑使用.sort() function 取决于最大/最小值,并获得结果的第一个元素。

But maybe you know better preforming solution?但也许您知道更好的预成型解决方案?

EDIT: Array size is below 1k elements.编辑:数组大小低于 1k 个元素。 By Min /max i meant alphabetic sort: first and last element. Min /max 我的意思是字母排序:第一个和最后一个元素。

Iterate through your list and update min and max in each iteration step遍历您的列表并在每个迭代步骤中更新 min 和 max

 function getMinMax(arr) { if (!arr) { return null; } var minV = arr[0]; var maxV = arr[0]; for (a of arr) { if (a < minV) minV = a; if (a > maxV) maxV = a; } return [minV, maxV]; } console.log(getMinMax(['abc', 'aaa', 'abb']));

It should be much faster than sort() for large arrays.对于大型数组,它应该比sort()快得多。 This algorithm is O(n) while sort() is at least O(n log(n)).这个算法是 O(n) 而sort()至少是 O(n log(n))。

Here are a few solutions to wrap your head around:这里有一些解决方案可以让你头脑清醒:

Min/max based on length基于长度的最小值/最大值

const output = arr.sort((a, b) => a.length - b.length);

Min/max as in alphabetical order按字母顺序排列的最小值/最大值

const output = arr.sort();

Extract the minimum and max提取最小值和最大值

const max = arr.sort(() => 1)[0];

If you don't want to use a sort,.如果你不想使用排序,.

Another option is to use Array.reduce to maintain the min & max values.另一种选择是使用Array.reduce来维护minmax

Below is a working snippet showing this.下面是一个显示这一点的工作片段。

ps.附: Your test data already had the min as the first element, and max as the last element, so I've altered the example array to have zzz , that of course would be max .您的测试数据已经将 min 作为第一个元素,将 max 作为最后一个元素,因此我已将示例数组更改为zzz ,这当然是max

 var arr = ['aab','aac','zzz','aad','abx']; var ret = arr.reduce((a, v) => { a.min = a.min === null ? v : v.localeCompare(a.min) < 0 ? v : a.min; a.max = a.max === null ? v : v.localeCompare(a.max) > 0 ? v : a.max; return a; }, {min: null, max: null}); console.log(ret);

To extend @Keith's answers.. if we want min or max only then reduce will be 1-liner.扩展@Keith 的答案.. 如果我们只想要minmax ,则reduce将是 1-liner。

var arr = ['aab','aac','aad','abx']
var min = arr.reduce((min,c) => c < min? c : min) // 'aab'
var max = arr.reduce((max,c) => c > max? c : max) // 'abx'

You can use reduce in one pass but have to check what to return if the array is empty (currently returns undefined for both min and max)您可以一次性使用reduce,但必须检查数组是否为空时要返回的内容(当前对于min 和max 都返回undefined)

 const arr = [ 'aab', 'aac', 'aad', 'abx', ]; console.log( arr.reduce( ([min, max], item) => [ min.localeCompare(item) > 0 ? item : min, max.localeCompare(item) < 0 ? item : max, ], [arr[0], arr[0]], ), );

This is what I did (note that it mutates the original array):这就是我所做的(注意它改变了原始数组):

 const myArray = ['beta', 'alpha', 'zeta', 'delta']; const min = myArray.sort()[0]; const max = myArray.reverse()[0]; console.log({ min, max });

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

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