使用 map 函数验证数据
validate data using map function
我想使用 map.I 验证数据 map.I 想获取密钥名称并检查一些条件
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.`;
}
}
只需使用键 field
从您的对象中检索值:
const obj = {
firstName: ['errorFirstName', 'msgFirstName'],
lastName: ['errorLastName', 'msgLastName'],
middleName: ['errorMiddleName', 'msgMiddleName'],
}
Object.keys(obj).forEach(fieldName => {
if (this[fieldName].length === 0) {
// your logic here
const [hasError, msg] = obj[fieldName];
this[hasError] = true;
this[msg] = `${fieldName} is required.`;
}
}
我想使用 map.I 验证数据 map.I 想获取密钥名称并检查一些条件
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.`;
}
}
只需使用键 field
从您的对象中检索值:
const obj = {
firstName: ['errorFirstName', 'msgFirstName'],
lastName: ['errorLastName', 'msgLastName'],
middleName: ['errorMiddleName', 'msgMiddleName'],
}
Object.keys(obj).forEach(fieldName => {
if (this[fieldName].length === 0) {
// your logic here
const [hasError, msg] = obj[fieldName];
this[hasError] = true;
this[msg] = `${fieldName} is required.`;
}
}