多次元オブジェクトをループして、キーが別のオブジェクトに存在するかどうかをテストする再帰関数を作成しようとしています。キーが存在しない場合はループを解除してfalseを返し、すべてのキーが存在する場合はtrueを返します。
私が抱えている問題は、関数が常にtrueを返しているように見えることです。これが私が使用しているコードです:
var properties = {'global': {'structure' : {'body': {}}}};
var testExists = {'global': {'structure': {'test': 'value'}}};
if( ! this.exists(properties, testExists)) {
console.log("DOESNT EXIST");
}
exists: function(destination, source) {
var exists = true;
check:
for (var property in source) {
if(destination[property]) {
arguments.callee(destination[property], source[property]);
}
else
{
exists = false;
break check;
}
}
console.log(exists);
return exists;
},
コンソールを表示して「exists」の値を確認すると、最初のfalseと2番目のtrueの2行が表示されるため、作成している再帰にエラーがあるはずです。