Javascript递归对象迭代生成表单元素名称

Javascript递归对象迭代生成表单元素名称,javascript,forms,loops,recursion,logic,Javascript,Forms,Loops,Recursion,Logic,我有一个AJAX响应,其中包含一个对象,该对象存储了我的所有表单元素结构及其相应的错误消息(如果有)。 此对象结构按如下方式构建: var obj = { 'entity1': { //Entity 'attribute1': { //Field 'isEmpty': "This value is required" //rule with error message }, 'attribute2': { //Fie

我有一个AJAX响应,其中包含一个对象,该对象存储了我的所有表单元素结构及其相应的错误消息(如果有)。 此对象结构按如下方式构建:

var obj = {
    'entity1': { //Entity
        'attribute1': { //Field
            'isEmpty': "This value is required" //rule with error message
        },
        'attribute2': { //Field
            'isEmpty': "This value is required" //rule with error message
        }
    },
    'entity2': { //Entity
        'attribute1': { //Field
            'isEmpty': "This value is required" //rule with error message
        },
        'attribute2': { //Field
            'isEmpty': "This value is required" //rule with error message
        }
    }
};
<input name="mainentity[entity1][attribute1]" />
<input name="mainentity[entity1][attribute2]" />
<input name="mainentity[entity2][attribute1]" />
<input name="mainentity[entity2][attribute2]" />
...
所有这些字段都以主实体包围的形式存储,如下所示:

var obj = {
    'entity1': { //Entity
        'attribute1': { //Field
            'isEmpty': "This value is required" //rule with error message
        },
        'attribute2': { //Field
            'isEmpty': "This value is required" //rule with error message
        }
    },
    'entity2': { //Entity
        'attribute1': { //Field
            'isEmpty': "This value is required" //rule with error message
        },
        'attribute2': { //Field
            'isEmpty': "This value is required" //rule with error message
        }
    }
};
<input name="mainentity[entity1][attribute1]" />
<input name="mainentity[entity1][attribute2]" />
<input name="mainentity[entity2][attribute1]" />
<input name="mainentity[entity2][attribute2]" />
...
我基于属性的类型。如果
字符串
,那么它就是一条消息。否则,它是递归中需要考虑的字段。我尝试使用标志
isChild
,只是为了知道何时确切地打破
partialName
,并作为一个新属性重新开始

问题是最终结果是这样的:

{
    'mainEntity[entity1][attribute1]': ["This value is required"],
    'mainEntity[entity1][attribute1][attribute2]': ["This value is required"], //wrong result, attribute1 shouldn't be here
    'mainEntity[entity2][attribute1]': ["This value is required"],
    'mainEntity[entity2][attribute1][attribute2]': ["This value is required"], //wrong result, attribute1 shouldn't be here
}
我希望:

{
    'mainEntity[entity1][attribute1]': ["This value is required"],
    'mainEntity[entity1][attribute2]': ["This value is required"], //No attribute 1 here
    'mainEntity[entity2][attribute1]': ["This value is required"],
    'mainEntity[entity2][attribute2]': ["This value is required"], //No attribute 1 here
}
我理解这个问题。
isChild
标志仅适用于更高级别,并且考虑到在我的逻辑中,从递归开始的所有内容都是子对象,
partialName
无法正常工作


如何使此函数按预期工作?我的意思是,我如何知道正确的时间将部分名称分解为新的输入,而不是连接到错误的先前字段,并使其适用于任何具有N个嵌套响应对象的情况?

此替代方法递归循环并连接来自对象的每个新找到的键

var obj={'entity1':{'attribute1':{'isEmpty':“此值需要1”},'attribute2':{'isEmpty':“此值需要2”},'entity2':{'attribute1':{'isEmpty':“此值需要3”},'attribute2':{'isEmpty':“此值需要4”};
函数循环(o、currentPath、result){
Object.keys(o).forEach(函数(k){
if(typeof o[k]=='string')结果[`${currentPath}`]=[o[k]];
else循环(o[k],`${currentPath}[${k}]`,结果);
});
}
var result={};
循环(obj,‘主实体’,结果);
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
非常简单的解决方案,非常感谢!我只是做了一些小的调整,以适应输入有多条消息的情况!