I have the below JSON and need to iterate thru the “rule” object. I can never seems to call it correctly to be able access more than just the last rule.
If someone could point me in the right direction, i’d appreciate it
var rules = [{
"rule":[{
"fields":["CASE.PROSECUTED_FLAG","CASE.RESTITUTION_SIGNED"],
"conditions":["N","Y"],
"actions":"required",
"action_fields":["test1","test2"]
}],
"rule":[{
"fields":["CASE.PROSECUTED_FLAG","CASE.RESTITUTION_SIGNED"],
"conditions":["N","N"],
"actions": "required",
"action_fields": ["test3", "test4"]
}],
"rule":[{
"fields":["CASE.PROSECUTED_FLAG"],
"conditions":["Y"],
"actions": "required",
"action_fields": ["test5"]
}]
}];
function rulesControl() {
alert(rules.length);
for (var rule in rules) {
var xfields = rules[rule].rule.fields;
var conditions = rules[rule].rule.conditions;
var actions = rules[rule].rule.actions;
var action_fields = rules[rule].rule.action_fields;
//test the fields (if success, mark them, else clear them)
if (validateRule(xfields, conditions)) {
rulesAction(actions, action_fields);
} else {
ruleActionMakeClear(action_fields);
}
}
}
That is because that JSON is oddly formed. It defines the property “rule” on teh containing object three times, in essence overwriting itself. This is probably more what you are after, rather than having all those single item arrays nested in objects.
You would access this as:
Where i is the index value for the rule and PROPERTY is fields, conditions, etc.