gpt4 book ai didi

Dart - 从列表中获取最近(更大)的值?

转载 作者:行者123 更新时间:2023-12-03 02:52:34 27 4
gpt4 key购买 nike

如何在列表中找到最接近的值,这将返回更高的值?示例:[3,7,12,19] 的列表,如果我的值为 8,我如何获得最接近(更大)的值 12?我想要 dart 中的这个逻辑。

最佳答案

只需过滤 List 仅针对大于或等于您的数字的值并获得最低值:

var n = 8; // Number to match
var l = [3, 7, 12, 19]; // List of values

var greater = l.where((e) => e >= n).toList()..sort(); //List of the greater values

print(greater.first); // Print the first value. -> 12

关于Dart - 从列表中获取最近(更大)的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60525296/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com