【问题标题】:Iterate through and array which contains another array javascript遍历包含另一个数组javascript的数组
【发布时间】:2021-08-07 03:08:41
【问题描述】:
我从发布请求中返回了以下对象
"{\"Success\":false,\"Errors\":{\"Name\":[\"The Name field is required.\"],\"Id\":[\"The Id field is required.\"],\"SortOrder\":[\"The SortOrder field is required.\"]}}"
我需要遍历 Errors 对象并找到 Id 字段并在我的页面上突出显示它。我该怎么做呢?我可以遍历一个简单的数组,但这个响应看起来像是一个包含另一个数组的数组。
【问题讨论】:
标签:
javascript
jquery
asp.net
json
asp.net-mvc
【解决方案1】:
只需使用JSON.parse 将字符串解析为JavaScript 对象,然后像这样检索Id:
const response = "{\"Success\":false,\"Errors\":{\"Name\":[\"The Name field is required.\"],\"Id\":[\"The Id field is required.\"],\"SortOrder\":[\"The SortOrder field is required.\"]}}";
const parsed = JSON.parse(response);
console.log(parsed.Errors.Id[0]);