0

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

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

that was a dangerously quick answer and saves my day. thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.