[TIL] every & some

JIEUN YANG·2023년 2월 7일
0

every

every() checks if every of element in array is satisfied with a given condition. When meeting statement, this function returns true and vice versa.
So, only when all of elements pass the test, it returns true.

some

some() checks if any of element in array is satisfied with a given condition. When meeting statement, this function returns true and vice versa.




Example

as-is

 const valid =
    state.companyUsersList.filter(user => Object.values(user).includes(''))
      .length === 0 &&
    state.companyUsersList.filter(user => user.isDefault).length === 1

to-be

  const valid =
    state.companyUsersList.every(user => !Object.values(user).includes('')) &&
    state.companyUsersList.some(user => user.isDefault)

By using every and some, no need to add length property to get Boolean type. It can be simpler and cleaner code!

profile
violet's development note

0개의 댓글