Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

validate data using map function

I would like to validate data using map.I would like to get the key name and check some conditions

const obj = {
    firstName: ['errorFirstName', 'msgFirstName'],
    lastName: ['errorLastName', 'msgLastName'],
    middleName: ['errorMiddleName', 'msgMiddleName'],
  }
  if (Object.keys(obj).includes(field)) {
 //I would like to pass here the object key length for each key :example if(this.firstName.length === 0)
    if(this[obj[key]].length === 0) {
      const [hasError, msg] = obj[field];
      this[hasError] = true;
      this[msg] = `${field} is required.`;
    }
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Simply use the key field to retrieve the value from your object:

const obj = {
    firstName: ['errorFirstName', 'msgFirstName'],
    lastName: ['errorLastName', 'msgLastName'],
    middleName: ['errorMiddleName', 'msgMiddleName'],
  }
  if (Object.keys(obj).includes(field)) {
    if (obj[field].length === 0) {
      // your logic here
      const [hasError, msg] = obj[field];
      this[hasError] = true;
      this[msg] = `${field} is required.`;
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading