每个...休息 [英] for each ... break

查看:54
本文介绍了每个...休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我从for-each构造中断开(PHP / Javascript)时我都感到很脏

I feel dirty every time I "break" out of a for-each construct (PHP/Javascript)

所以这样的事情:

// Javascript示例

// Javascript example

for (object in objectList)
{
   if (object.test == true)
   {
      //do some process on object
      break;
   }

}

对于大型对象列表,我会通过麻烦建立一个更优雅的解决方案。但对于小型列表,没有明显的性能问题,因此为什么不呢?它很快,更重要的是易于理解和遵循。

For large objectLists I would go through the hassle building a more elegant solution. But for small lists there is no noticeable performance issue and hence "why not?" It's quick and more importantly easy to understand and follow.

但它只是感觉不对。有点像goto语句。

But it just "feels wrong". Kind of like a goto statement.

你如何处理这种情况?

推荐答案

我休息一下。这是一个完美的解决方案。

I use a break. It's a perfectly cromulent solution.

这篇关于每个...休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆