我从服务器收到一个有效的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);
        });
    }

最佳答案

errorerrors对象中的键。您必须使用它来访问errors中的数组。

for (var error in errors) {
  errors[error].forEach(function(message) {
    console.log(message);
  });
}

关于javascript - 嵌套循环JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32766237/

10-11 13:38
查看更多