2

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

4
  • 1
    What did you try? in any case, you may want to use Object.values(err).some(e => e !== '' && e !== null && e !== undefined); or something like that. if Object.values is not support, just revert to object.keys: Object.keys(err).some(e => err[e] !== null && err[e] !== undefined && err[e] !== ''); (assuming the error is present whenever any key is defined and not empty). Commented Jun 22, 2018 at 16:50
  • i wasn't aware of .some(), that should work. thank you very much Commented Jun 22, 2018 at 16:53
  • 1
    remember to include the polyfill, since it's not a standard of every browser, so you might encounter some browser not having the correct ES version to use it. Commented Jun 22, 2018 at 16:56
  • lol, didn't know what a polyfill was, thanks a ton for that Commented Jun 22, 2018 at 17:03

6 Answers 6

4

You can use Array.some() on the Object.values() to check if there are some error messages

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

const obj = {name: "Naimur", age: 21}

console.log("name" in obj) => true

console.log("age" in obj) => true

console.log("location" in obj) => false

Comments

0

Maybe like this:

var data={
  err: {
    subject: '',
    body: '',
    recipient: ''
  },
};

if(data.err.subject!='' || data.err.body!='' || data.err.recipient!=''){
	console.log('ERROR!');
}else{
	console.log('ALL RIGHT!');
}

1 Comment

this was my solution at first, but i thought there should be a better method
0
var err =  {
  subject: 'some error',
  body: 'message',
  recipient: 'Tom'
};

Object.values(err) will return an array Array ["some error", 'message', 'Tom']. Now, you can check the values of this array and then disable the button if these values satisfy some conditions.

Hope it helps.

Comments

0

I'm assuming that all of the elements have to equal '' for there to be a valid error. So you can use the .every() method to check every element in the returned array. If only some of the elements have to equal '' for there to be an error message, just user .some() instead.

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

console.log(!Object.values(err).every(e => e === '')) //returns false meaning there isn't an error message

Comments

0

you may use .reduce() to return the sum of errors and also check if err has values with this code

if( (Object.values(err).length) ? Object.values(err).reduce((ac, next) => ac + next.length, 0) :false ){
  console.log( 'there is errors' );   
}

2 Comments

In this example is there a time when Object.values(err).length will return anything but 3? Assuming of course you don't delete keys/values or add more?
@Matt im asuming this should work with any amount of keys an also not pass the if statement if err is empty {}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.