Answers for "Javascript Object [key] Shorthand"

0

Javascript Object [key] Shorthand

// object validation rules
const schema = {
  first: {
    required:true
  },
  last: {
    required:true
  }
}

// universal validation function
const validate = (schema, values) => {
  for(field in schema) {
    if(schema[field].required) {
      if(!values[field]) {
        return false;
      }
    }
  }
  return true;
}

console.log(validate(schema, {first:'Bruce'})); // false
console.log(validate(schema, {first:'Bruce',last:'Wayne'})); // true
Posted by: Guest on October-13-2021

Code answers related to "Javascript Object [key] Shorthand"

Code answers related to "Javascript"

Browse Popular Code Answers by Language