Determining if all attributes on a javascript object are null or an empty string

后端 未结 15 1807
走了就别回头了
走了就别回头了 2020-12-08 04:08

What is the most elegant way to determine if all attributes in a javascript object are either null or the empty string? It should work for an arbitrary number of attributes

15条回答
  •  死守一世寂寞
    2020-12-08 04:46

    Using Array.some() and check if the values are not null and not empty is more efficient than using Array.every and check it the other way around.

    const isEmpty = !Object.values(object).some(x => (x !== null && x !== ''));
    

    This answer should just make the excellent comment of user abd995 more visible.

提交回复
热议问题