Reputation: 3521
for example i have an object with validation rules
validationRules = {
planType: {
group: 'personalData',
required: true,
messages: {
required: 'Required Field'
}
},
name: {
group: 'personalData',
required: true,
pattern: /\w+\s+\w+/,
messages: {
required: 'Required Field',
pattern: 'Insert first and last names'
}
},
}
I need to validate a form wizard by steps so I need to make a function just to validate each step
function isStepValid() {
console.log(lastActiveNav);
const currentStep = lastActiveNav.getAttribute('data-pane');
var stepRules = validationRules.filter(currentStep); // wont work cause not an array
console.log(stepRules); // this is the value in the group property in the validationRules
for (let key in validationRules) {
}
}
I want to loop only through the properties with the value in the group property that match. Sadly i can only find an answer using an array with filter.
Upvotes: 0
Views: 28
Reputation: 301
const currentStep = lastActiveNav.getAttribute('data-pane');
Object.keys(validationRules)
.map(key => validationRules[key])
.filter(validationRule => validationRule.group === currentStep)
.forEach(validationRule => { // forEach/map/reduce depending what you want to do
// Code here
})
Upvotes: 1