繁体   English   中英

如何在AngularJs中获取json对象和嵌套对象属性名称和值?

[英]How to get json object and nested object Property names and values in AngularJs?

我有一个像这样的json对象

 { "from": { "required": false, "type": { "name": { "required": false, "type": "String" }, "email": { "required": true, "type": "String" } } }, "to": { "required": true, "type": [ { "name": { "required": false, "type": "String" }, "email": { "required": true, "type": "String" } } ] }, "subject": { "required": true, "type": "String" }, "text": { "required": true, "type": "String" }, "html": { "required": true, "type": "String" } } 

该对象包含嵌套对象,具有从各种服务设置的属性。 因此,属性名称是动态更改的。 我想获得属性名称和各自的值。 我已经尝试过以下代码。

 for (var prop in data) {
                $scope.fieldsInfo.push({ "label": prop, "required":data.hasOwnProperty(prop) });
            }

在上面的代码我也获取属性名称(从)和所需的值,现在我想获得名称对象内的嵌套对象属性名称(名称,电子邮件)和(必需,类型)值。 另外,在上述对象from包含type是这样一个对象isArray:false和下一个对象to包含type是一个阵列,使得isArray:true剩余的对象type是一个字符串,以便isArray:false 输出像这样

 $scope.fieldsInfo=[]; $scope.fieldsInfo=[ { "label" :"from", "required" :false, "isArray" :false, "type" : [ { "label" :"name", "type" : "String", "required": false } { "label" : "email", "type" : "String", "required": true } ] }, { "label" :"to", "required" :true, "isArray" :true, "type" : [ { "label" : "name", "type" : "String", "required": false } { "label" : "email", "type" : "String", "required": true } ] }, { "label" :"subject", "required" :true, "isArray" :false, "type" :"String" }, { "label" :"text", "required" :true, "isArray" :false, "type" :"String" }, { "label" :"html", "required" :true, "isArray" :false, "type" :"String" } ] 

我不确定你想要什么,但根据你的问题的标题,我将提供一些处理javascript对象/数组的基础知识:

使用for (var prop in data) {} (与你一样)迭代键不适用于嵌套目标。 一种可能的方法是使用递归函数:

function get_keys_recursive (object_or_array) {

    for (var key in object)
        if (object[key] != null) //null is also a object
            if (typeof object[key] === 'object') 
                get_keys_recursive (typeof object[key]); //recursive function call
            else {

                console.log (key); //here you get all keys
                console.log (object[key]); //here you get the value 

            }

}

get_keys_recursive (your_object); //function call

如果没有回答您的问题,请提供更详细的信息。

$scope.fieldsInfo = [];
            $scope.type = [];
            for (var prop in data) {
                var isArray = angular.isArray(data[prop].type);
                var isObject = angular.isObject(data[prop].type);
                if (isArray) {
                    for (var nestedProp in data[prop].type[0]) {
                        $scope.type.push({ "label": nestedProp, "required": result.data.hasOwnProperty(nestedProp), "type": data[prop].type[0][nestedProp].type });
                    }
                    $scope.fieldsInfo.push({ "label": prop, "required": result.data.hasOwnProperty(prop), "isArray": isArray, "type": $scope.type });
                    $scope.type = [];
                }
                else
                    if (isObject) {
                        for (var nestedProp in data[prop].type) {
                            $scope.type.push({ "label": nestedProp, "required": data.hasOwnProperty(nestedProp), "type": data[prop].type[nestedProp].type });
                        }
                        $scope.fieldsInfo.push({ "label": prop, "required": result.data.hasOwnProperty(prop), "isArray": isArray, "type": $scope.type });
                        $scope.type = [];
                    }
                else {
                    $scope.fieldsInfo.push({ "label": prop, "required": data.hasOwnProperty(prop), "isArray": isArray, "type": data[prop].type });
                }
            }

暂无
暂无

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

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