javascript - Search for a value in nested objects -


i have check if object contains value in javascript , don't know how it. object looks this:

match {   player1: undefined,   player2: undefined,   childrenleft:     match {      player1: 'john',      player2: 'mary',      childrenleft: undefined,      childrenright: undefined },   childrenright:     match {      player1: 'michael',      player2: 'james',      childrenleft: undefined,      childrenright: undefined }   } 

now it's competition final , 2 semi-finals bigger depending on number of players need traverse tree. have function supposes return next opponent doesn't work when search players on childrenleft. so, when execute match.nextopponent(james) got 'michael' result when execute match.nextopponent(mary) got 'undefined'.

match.prototype.nextopponent = function (player) {     if (this.player1 === player) return this.player2;     if (this.player2 === player) return this.player1;     if (!this.player2 && !this.player1 && this.childrenright !== undefined) return this.childrenright.nextopponent(player);     if (!this.player1 && !this.player2 && this.childrenleft !== undefined) return this.childrenleft.nextopponent(player); } 

anyone help? thank much

the problem occurs because return in last 2 if blocks, when return value recursive call undefined. means if first if condition true, there no way second if condition tried.

so resolve this, use code:

match.prototype.nextopponent = function (player) {     if (this.player1 === player) return this.player2;     if (this.player2 === player) return this.player1;     var match;     if (!this.player2 && !this.player1 && this.childrenright)          match = this.childrenright.nextopponent(player);     // maybe previous return value undefined, try other side:     if (!match && !this.player1 && !this.player2 && this.childrenleft)          match = this.childrenleft.nextopponent(player);     return match; } 

function match(p1, p2) {      this.player1 = p1;      this.player2 = p2;      this.childrenleft = undefined;      this.childrenright = undefined;  }    match.prototype.nextopponent = function (player) {      if (this.player1 === player) return this.player2;      if (this.player2 === player) return this.player1;      var match;      if (!this.player2 && !this.player1 && this.childrenright)           match = this.childrenright.nextopponent(player);      if (!match && !this.player1 && !this.player2 && this.childrenleft)           match = this.childrenleft.nextopponent(player);      return match;  }    var root = new match();  root.childrenleft = new match('john', 'mary');   root.childrenright = new match('michael', 'james');     console.log('james plays: ', root.nextopponent('james'));  console.log('mary plays: ', root.nextopponent('mary'));

nb: don't have compare childrenleft undefined. undefined falsy , object truthy, can evaluate childrenleft in if condition.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -