简体   繁体   中英

lodash's min returning wrong value from an array of negative values

I have this array of negative values:

["-117.911732", "-112.0756724", "-71.0595678", "-78.8783689", "-114.0833", "-78.6390779", "-87.6244212", "-104.9847034", "-83.0007065", "-96.7968994", "-83.0567375", "-113.5000", "-80.1936589", "-118.2427266", "-93.1015026", "-73.5833", "-86.78086153", "-74.1723667", "-73.9865812", "-73.9865812", "-75.7000", "-75.163789", "-79.9900861", "-121.8831349", "-90.1978889", "-82.458444", "-79.4167", "-123.1333", "-77.0366456", "-97.1667"]

Lodash's _.min function returns -104.9847034 instead of -123.1333 which is obviously wrong? What do I miss?

Here is the running example: http://codepen.io/neptune01/pen/XMOeVM

You have array of string so you need to first convert strings to numbers using _.map .

 var arr = ["-117.911732", "-112.0756724", "-71.0595678", "-78.8783689", "-114.0833", "-78.6390779", "-87.6244212", "-104.9847034", "-83.0007065", "-96.7968994", "-83.0567375", "-113.5000", "-80.1936589", "-118.2427266", "-93.1015026", "-73.5833", "-86.78086153", "-74.1723667", "-73.9865812", "-73.9865812", "-75.7000", "-75.163789", "-79.9900861", "-121.8831349", "-90.1978889", "-82.458444", "-79.4167", "-123.1333", "-77.0366456", "-97.1667"]; console.log(_.min(_.map(arr, Number))) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script> 

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