gpt4 book ai didi

flutter - 如何在Dart/Flutter中迭代2个不同数组中的每个值?

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

请提供有关Dart逻辑的帮助,
我有2个数组:

option1 = ["green", "yellow", "purple"
option2 = [32,33]
我想将它们迭代为:
mergedArray = [["green", 32], ["green", 33], ["yellow", 32], ["yellow",33], ["purple",32], ["purple",33]]
帮助将不胜感激,谢谢🙏

最佳答案

您可以为此使用两个for循环。循环遍历option2,然后在其内部循环遍历option1。在option1的for循环内,您可以将其添加到列表中。

void main() {
final List<String> option1 = ["green", "yellow", "purple"];
final List<int> option2 = [32, 33];
List<List<dynamic>> myList=[];
for (var item2 in option2) {
for (var item1 in option1) {
myList.add([item1, item2]);
}
}
print(myList);
}
输出:
[[green, 32], [yellow, 32], [purple, 32], [green, 33], [yellow, 33], [purple, 33]]

关于flutter - 如何在Dart/Flutter中迭代2个不同数组中的每个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63987729/

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