用地图替换循环

发布于 2025-02-05 06:41:07 字数 122 浏览 3 评论 0原文

我正在尝试在我的代码中删除for循环,并尝试使用地图和功能。对于循环,我的代码正常工作,但我不知道如何使用地图以及在哪里功能获得相同的输出。我尝试了,但是在地图中创建类实例时,我会遇到错误。我不知道在地图中写什么以及在哪里起作用。

I am trying to remove the for loop in my code and trying to use the map and where function. With for loop my code is working fine but I don't know how to use the map and where function to get the same output. I tried but I am getting errors while creating an instance of class inside the map. I don't know what to write inside the map and where functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘和柠 2025-02-12 06:41:08

基于循环和(如果),您可以直接使用列表来添加到列表:

final List<NewCustomerFav> customFavartList = [ 
  for (final article in articleList) 
    for (final customArticle in customerFavList) 
      if (article.id == customArticle.articleId) 
        NewCustomerFav(customArticle.id, article,customArticle.like)
];

listeral也可以使用MAP编写。展开其中
几种方法,但没有一个直接。

棘手的一点是您正在做两个循环,然后是IF。
一种方法是:

final List<NewCustomerFav> customFavartList = articleList
    .expand((article) => customerFavList
        .where((customArticle) => article.id == customArticle.id)
        .map((customArticle) =>
            NewCustomerFav(customArticle.id, article, customArticle.like)))
    .toList();

这使用展开,因为内部地图可以创建多个(或零)结果。那不是地图擅长的东西。

另一种方法可能是首先创建所有对,然后对它们进行过滤:

class Pair<A, B> {
  final A first;
  final B second;
  Pair(this.first, this.second);
}
// ...
final List<NewCustomerFav> customFavartList = articleList
    .expand((article) =>
        customerFavList.map((customArticle) => Pair(article, customArticle)))
    .where((pair) => pair.first.id == pair.second.id)
    .map((pair) => NewCustomerFav(pair.second.id, pair.first, pair.second.like))
    .toList();

更紧密地遵循Listeral的逻辑,在两个文章列表上进行循环,然后对成对进行过滤。
它不那么可读,需要一个辅助类型来表示对。

总而言之,列表的文字符号更可读,并且使用map展开wenty < /code>和tolist。 (有时候,它只需要您将更复杂的计算移动到辅助功能中,这可以说也可以提高可读性。)

简而言之,有时您需要Expand以及map 在转换列表中将列表创建到峰值方法调用时。

Adding to a list, based on a loop and an if, is something you can do directly with a list literal:

final List<NewCustomerFav> customFavartList = [ 
  for (final article in articleList) 
    for (final customArticle in customerFavList) 
      if (article.id == customArticle.articleId) 
        NewCustomerFav(customArticle.id, article,customArticle.like)
];

That listeral can also be written using map, expand and where in a
few ways, but none will be as direct.

The tricky bit is that you are doing two loops, then the if.
One approach would be:

final List<NewCustomerFav> customFavartList = articleList
    .expand((article) => customerFavList
        .where((customArticle) => article.id == customArticle.id)
        .map((customArticle) =>
            NewCustomerFav(customArticle.id, article, customArticle.like)))
    .toList();

This uses expand because the inner map can create multiple (or zero) results. That's not something map is good at.

Another approach could be creating all the pairs first, then filtering them:

class Pair<A, B> {
  final A first;
  final B second;
  Pair(this.first, this.second);
}
// ...
final List<NewCustomerFav> customFavartList = articleList
    .expand((article) =>
        customerFavList.map((customArticle) => Pair(article, customArticle)))
    .where((pair) => pair.first.id == pair.second.id)
    .map((pair) => NewCustomerFav(pair.second.id, pair.first, pair.second.like))
    .toList();

That more closely follows the logic of the listeral, doing loops over the two article lists, then filtering on the pairs.
It's not as readable, and requires a helper type to represent the pair.

All in all, the list literal notation is more readable and as powerful as anything you can do with map, expand, where and toList. (Sometimes it just requires you to move more complicated computations into helper functions, which can, arguably, improve readability too.)

In short, sometimes you need expand as well as map and where when converting loops that create lists into iterable method calls.

无所的.畏惧 2025-02-12 06:41:08

请检查以下两种不同的方法。

第一个

articleList.forEach(
  (article) => customerFavList.forEach(
    (customerFav) {
      if (article.id == customerFav.articleId) {
        final fav = NewCustomerFav(customerFav.id, article, 
          customerFav.like);
        customFavartList.add(fav);
      }
    },
  ),
);

第二

articleList.forEach(
  (article) => customerFavList
      .where(
    (customerFav) => article.id == customerFav.articleId,
  )
      .forEach((newCustomerFav) {
    final fav =
        NewCustomerFav(newCustomerFav.id, article, newCustomerFav.like);
    customFavartList.add(fav);
  }),
);

第三

final newCustomerFavListList = articleList.map(
  (article) {
    final result = customerFavList.where(
      (customerFav) {
        if (customerFav.articleId == article.id) return true;

        return false;
      },
    ).toList();

    return result
        .map((item) => NewCustomerFav(item.id, article, item.like))
        .toList();
  },
).toList();

第四

articleList.map(
  (article) {
    final result = customerFavList.where(
      (customerFav) {
        if (customerFav.articleId == article.id) return true;

        return false;
      },
    ).toList();

    customFavartList.addAll(
        result.map((item) => NewCustomerFav(item.id, article, 
item.like)));
  },
).toList();

您可以通过

如果您有其他疑问,请随时写在评论中。

Please check the following two different approaches.

First

articleList.forEach(
  (article) => customerFavList.forEach(
    (customerFav) {
      if (article.id == customerFav.articleId) {
        final fav = NewCustomerFav(customerFav.id, article, 
          customerFav.like);
        customFavartList.add(fav);
      }
    },
  ),
);

Second

articleList.forEach(
  (article) => customerFavList
      .where(
    (customerFav) => article.id == customerFav.articleId,
  )
      .forEach((newCustomerFav) {
    final fav =
        NewCustomerFav(newCustomerFav.id, article, newCustomerFav.like);
    customFavartList.add(fav);
  }),
);

Third

final newCustomerFavListList = articleList.map(
  (article) {
    final result = customerFavList.where(
      (customerFav) {
        if (customerFav.articleId == article.id) return true;

        return false;
      },
    ).toList();

    return result
        .map((item) => NewCustomerFav(item.id, article, item.like))
        .toList();
  },
).toList();

Fourth

articleList.map(
  (article) {
    final result = customerFavList.where(
      (customerFav) {
        if (customerFav.articleId == article.id) return true;

        return false;
      },
    ).toList();

    customFavartList.addAll(
        result.map((item) => NewCustomerFav(item.id, article, 
item.like)));
  },
).toList();

You can access the complete source code through GitHub.

If you have further questions, please don't hesitate to write in the comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文