首页 > 解决方案 > How to check whether a javascript object contains any values while having keys

问题描述

I have a javascript object which contains error messages:

err: {
  subject: '',
  body: '',
  recipient: ''
},

i want to disable a submit button based on whether an error is present. in the above declaration, there are no errors present.

i'm aware of Object.values(err) but i do not know how to use the resulting list, which has a length of 3.

how would I do this? thank you

标签: javascript

解决方案


您可以使用Array.some()Object.values()检查是否有一些错误消息

let obj = {
  err: {
    subject: '',
    body: '',
    recipient: ''
  }
};

let someErrors = Object.values(obj.err).some(e => e.length);
console.log(someErrors);


推荐阅读