替换foreach google脚本

发布于 2025-02-09 19:11:29 字数 539 浏览 1 评论 0原文

我想为我替换foreach函数,以打破代码,以防我没有订单的客户。

当前代码:

customers.forEach(function (customer) 
{
  ss.toast(`Creating Invoice for ${customer.name}`, APP_TITLE, 1);
  let invoice = createInvoiceForCustomer(
    customer, products, transactions, invoiceTemplateSheet, ss.getId());
  invoices.push(invoice);
});

我要制作的代码:

    for (var i=1 ; i<=custlastrow ; i++)
  {
    if (customers.ordenes == "No")
    {
      continue; // Skip current iteration... 
    }
    do the other part
  }

I want to replace the forEach function for me to break the code in case i don't have a customer with an order.

Current code:

customers.forEach(function (customer) 
{
  ss.toast(`Creating Invoice for ${customer.name}`, APP_TITLE, 1);
  let invoice = createInvoiceForCustomer(
    customer, products, transactions, invoiceTemplateSheet, ss.getId());
  invoices.push(invoice);
});

Code I'm trying to make:

    for (var i=1 ; i<=custlastrow ; i++)
  {
    if (customers.ordenes == "No")
    {
      continue; // Skip current iteration... 
    }
    do the other part
  }

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

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

发布评论

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

评论(1

傻比既视感 2025-02-16 19:11:29

为了使生活更轻松,您可以在之前添加过滤器,这意味着您可以将foreach留下。

这样:

const customersWithOrders = customers.filter(customer => customer.ordenes !== "No")

customersWithOrders.forEach(....

我发现在循环中使用继续可能会导致凌乱的代码。 IMO以上更容易阅读。

To make life easier, you could add a filter before, meaning you can leave the forEach.

Like this:

const customersWithOrders = customers.filter(customer => customer.ordenes !== "No")

customersWithOrders.forEach(....

I find that using continue in loops can lead to messy code. IMO the above is easier to read.

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