This is JSON returned
var errorsObject = JSON.parse('{ "fieldErrors": {"name":["You must enter your name1"],"name":["You must enter your name2"],"age":["Only people ages 13 to 19 may take this quiz"]}}');
Below code first loop working fine where I get the key name like name, age etc. In second loop want to get the values of the key selected from outer loop. Issue is errors.fieldErrors[key]. I need to loop only key with the name at first and get 2 values.
$.each(errors.fieldErrors, function(key, val) {
alert("key->" + key); //Fine
$.each(errors.fieldErrors[key], function(index, val) {
alert("key->" + key + ", val->" + val);
});
});
An object cannot contain the same key twice, so what you are trying to do is impossible. The first message is lost when the JSON is parsed.
However, your values are arrays – why not change the code returning the JSON to make proper use of those arrays? And assuming it’s created by PHP code the fact that you can create such JSON smells like someone using string functions to create it instead of
json_encode– another thing that should be changed…