I receive a valid json object for error messages from the server. I would like to loop through each error and nicely display the error in the web page. Here is example of the json from the server
{
"contract.cashPrice":[
"Cash price is required"
],
"contract.cashDownPayment":[
"Cash down payment is required"
],
"contract.effectiveDate":[
"Effective date is required"
],
"contract.firstPaymentDate":[
"First payment date is required"
],
"contract.mode":[
"Mode is required"
],
"contract.numberOfPayments":[
"Number of payments is required"
],
"contract.financeChargePercent":[
"Finance charge percent is required"
]
}
when i loop through this error object, i am only able to receive the object property, but not the values. below is the stub code i have written so far. any help is greatly appreciated.
for(var error in errors){
error.forEach(function(message){
console.log(message);
});
}
asked Sep 24, 2015 at 16:15
elixir
1,4422 gold badges12 silver badges23 bronze badges
1 Answer 1
error is a key in the errors object. You have to use it to access the arrays in errors.
for (var error in errors) {
errors[error].forEach(function(message) {
console.log(message);
});
}
answered Sep 24, 2015 at 16:17
Mike Cluck
32.6k13 gold badges84 silver badges95 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
elixir
that was a dangerously quick answer and saves my day. thank you.
lang-js