You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for each 구문은 array에서만 사용할 수 있다. 인자로 callback 함수를 등록하여 배열 요소와 index에 접근해 사용할 수 있다. 성능은 좋지만 break을 지원하지 않는다. callback 함수의 return이 true든 false든 둘 다 continue 처리되어 루프를 전부 돌게 된다. for each 자체의 return은 undefined이다.
break을 사용하기 위해 try, catch를 사용할 수 있지만, 코드가 지저분해질 수 있다. 더 우아한 방법으로 for each 대신에 every나 some을 사용하면 된다.
두 메서드 모두 for each처럼 배열을 순회할 때 사용된다. 조건에 만족하는 값이 있는 경우(즉, callback 함수에 return 값이 존재하는 경우) 루프를 중지하고 every의 경우 메서드 자체에서 false를 return하고 some의 경우 메서드 자체에서 true를 return 한다.
Activity