我从服务器收到错误消息的有效json对象。我想遍历每个错误并很好地在网页中显示错误。以下是服务器的json示例
{
"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"
]
}
当我循环遍历此错误对象时,我只能接收对象属性,但不能接收值。下面是我到目前为止编写的存根代码。非常感谢任何帮助。
for(var error in errors){
error.forEach(function(message){
console.log(message);
});
}
答案 0 :(得分:4)
error
是errors
对象中的关键字。您必须使用它来访问errors
中的数组。
for (var error in errors) {
errors[error].forEach(function(message) {
console.log(message);
});
}