Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

arrays - Error in generating a valid json recursive javascript function

I have a function to generate an object which I'm transforming to a JSON later on:

function createSearchCriteria(payload) {
    var output = [];
    if (payload['searchCriteria'] != null) {
        for (var i = 0; i < payload['searchCriteria'].length; i++) {
            var content = payload['searchCriteria'][i];
            output[i] = {};
            if (content['grouping'] != null) {
                output[i]['groupOperator'] = content['grouping'];
                output[i]['searchCriteria'] = [];
                output[i]['searchCriteria'].push(createSearchCriteria(content))
            } else {
                output[i]['name'] = content['name'];
                output[i]['type'] = content['type'];
            }
        }
    }
    return output
}

The input payload for this method is also a JSON value parsed

payload = JSON.parse(request);

The input structure is almost the same as the output, the only difference is the "grouping" attribute, which in the output is called "groupOperator".

I have implemented my function recursive because we can have different levels of search criteria.

Even though the searchCriteria in the input has only one [] each.

input data

Why does each searchCriteria in the result comes with 2 pairs of squared brackets?

{
    "searchCriteria": [
        {
            "groupOperator": "AND",
            "searchCriteria": [
                [
                    {
                        "groupOperator": "OR",
                        "searchCriteria": [
                            [
                                {
                                    "name": "FirstName",
                                    "type": "string"
                                },
                                {
                                    "name": "LastName",
                                    "type": "string"
                                },
                                {
                                    "name": "MiddleName",
                                    "type": "string"
                                },
                                {
                                    "name": "Document",
                                    "type": "string"
                                },
                                {
                                    "name": "DOB",
                                    "type": "date"
                                },
                                {
                                    "name": "CdrId",
                                    "type": "string"
                                }
                            ]
                        ]
                    },
                    {
                        "groupOperator": "AND",
                        "searchCriteria": [
                            [
                                {
                                    "name": "Active",
                                    "type": "bool"
                                },
                                {
                                    "name": "Id",
                                    "type": "int"
                                },
                                {
                                    "name": "CountryId",
                                    "type": "int"
                                }
                            ]
                        ]
                    }
                ]
            ]
        }
    ],
    "groupOperator": "AND"
}

Thanks in advance for your help.

question from:https://stackoverflow.com/questions/65870936/error-in-generating-a-valid-json-recursive-javascript-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try

output[i]['searchCriteria'] = createSearchCriteria(content)

instead of

output[i]['searchCriteria'] = [];
output[i]['searchCriteria'].push(createSearchCriteria(content))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...