次のオブジェクトを解析する必要があります。
私が実装したコード (2) は部分的に機能します。(1) のコメントを参照してください。コードを修正する方法のヒントがあれば (2) いただければ幸いです。
私の目標は、ルート オブジェクトにエラー キーがあるときに parser_2 を呼び出すことです。
私はjqueryとアンダースコアを使用しています。
(1)
parser({
"errors": ["errro1"], // it should be passed to parser_2
// with the code I implemented it works
"children": {
"points": {
"errors": ["This value should not be blank.", "error2"]
},
"message": [], // it should be passed to parser
// with the code I implemented it does not work
// because it calls parser_2 instead of parser or nothing!
"recipient_id": {
"errors": ["This value should not be blank."]
}
}
});
(2)
parser = function (object) {
_.each(object, function (object, name) {
if (object.errors) {
var inputElement = element.find('[name="' + name + '"]');
//other code
} else if ($.isArray(object)) {
parser_2(object);
} else if ($.isPlainObject(object)) {
parser(object);
}
});
};