简体   繁体   English

如何在 JS 中循环遍历复杂的 object

[英]How to loop through a complex object in JS

This is my obj, I just want to loop through this and show the errors:这是我的 obj,我只想遍历它并显示错误:

var obj = {
    "error": {
        "errors": {
            "username": {
                "properties": {
                    "message": "username field is empty.",
                    "type": "required",
                    "path": "username"
                },
                "kind": "required",
                "path": "username"
            },
            "email": {
                "properties": {
                    "message": "email field is empty.",
                    "type": "required",
                    "path": "email"
                },
                "kind": "required",
                "path": "email"
            },
            "password": {
                "properties": {
                    "message": "password field is empty.",
                    "type": "required",
                    "path": "password"
                },
                "kind": "required",
                "path": "password"
            }
        },
        "_message": "User validation failed",
        "message": "User validation failed: username: username field is empty., email: email field is empty., password: password field is empty."
    }
}

I want to show the errors: properties.message but I'm having hard time, this is what I tried so far:我想显示错误: properties.message但我很难过,这是我迄今为止尝试过的:

for (var key in obj.error.errors) {
    for (var key2 in key.properties){
        for (var key3 in key2.message){
            console.log(key3)
        } 
    } 
}

But the console is blank.但是控制台是空白的。

In the loop you are getting the key, not the object itself.在循环中,您得到的是密钥,而不是 object 本身。 Also you don't need to add all those loops if your data is structured like that.此外,如果您的数据是这样的结构,您也不需要添加所有这些循环。 Just pick the errors, iterate through all.只需选择错误,遍历所有错误。

for(var d in obj.error.errors){
    console.log(a[d].properties.message)
}

a working solution一个可行的解决方案

Object.values(obj.error.errors).forEach((error) => { console.log(error.properties.message) })

Looking to your data structure, you only need to iterate on the 'errors' list查看您的数据结构,您只需要迭代“错误”列表

Check this out, it will return you in the properties:message format检查一下,它将以properties:message格式返回您

 const obj = { "error": { "errors": { "username": { "properties": { "message": "username field is empty.", "type": "required", "path": "username" }, "kind": "required", "path": "username" }, "email": { "properties": { "message": "email field is empty.", "type": "required", "path": "email" }, "kind": "required", "path": "email" }, "password": { "properties": { "message": "password field is empty.", "type": "required", "path": "password" }, "kind": "required", "path": "password" } }, "_message": "User validation failed", "message": "User validation failed: username: username field is empty., email: email field is empty., password: password field is empty." } }; const objVal = Object.values(obj.error.errors); const errors = objVal.reduce((obj, item) => { return (obj[item.path] = item.properties.message, obj) },{}) console.log(errors);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM