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

后端 未结 15 1823
走了就别回头了
走了就别回头了 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:59

    Also if you are searching for only values are empty within the object,

    Object.values({ key: 0, key2: null, key3: undefined, key4: '' }).some(e => Boolean(e))
    // false
    
    Object.values({ key: 0, key2: null, key3: undefined, key4: "hello" }).some(e => Boolean(e))
    // true
    
    Object.values({ key: 1, key2: "hello" }).some(e => Boolean(e))
    // true
    

提交回复
热议问题