颤音/飞镖:如何计算列表阵列的标签
如何计算数组的标签? 我有此代码:
class Product{
Product({required this.name, required this.tags});
final String name;
final List<String> tags;
}
void TagList(){
final product =[
Product(name: 'bmw', tags: ['car', 'm3']),
Product(name: 'kia', tags: ['car', 'morning', 'suv']),
Product(name: 'hyundai', tags: ['car', 'ev6', 'suv']),
];
}
如何获得每个标签使用多少次?
预期输出:
汽车(3) M3(1) EV6(1) SUV(2) 早上(1)
How can I count tags of an array?
I have this code:
class Product{
Product({required this.name, required this.tags});
final String name;
final List<String> tags;
}
void TagList(){
final product =[
Product(name: 'bmw', tags: ['car', 'm3']),
Product(name: 'kia', tags: ['car', 'morning', 'suv']),
Product(name: 'hyundai', tags: ['car', 'ev6', 'suv']),
];
}
How can I get how many times each tag was used?
Expected output:
car(3)
m3(1)
ev6(1)
suv(2)
morning(1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用此功能在
products
列表上循环循环,并使用每次使用标签
的次数:输出:
这是一个完整的可运行示例:
You can use this function to loop over the
products
list and get how many times eachtag
is used:Output:
Here's a complete runnable example: