2

I'm trying to find a solution to make a loop (Javascript) until the array of object is empty. Here the object that I want to use :

"chain": {
    "evolves_to": [{
        "evolves_to": [{
            "evolves_to": [],
            "species": {
                "name": "nidoqueen"
            }
        }],
        "species": {
            "name": "nidorina"
        }
    }],
    "species": {
        "name": "nidoran-f"
    }
}

I would like to loop until to find the variable evolves_to empty. and in each loop using the species.name, to list the evolution, in my case : nidoran-f -> nidorina -> nidoqueen

I can not find yet a good way to do it. A bit lost. Thank you for your help ;)

2
  • For what do you want to use the variable species.name? Commented Apr 18, 2022 at 9:56
  • I want to list the evolution : for my example it ll be : Nidoran-f -> Ndorina -> Nidoqueen Commented Apr 18, 2022 at 9:58

2 Answers 2

2

You can use a recursive function:

const chain = {
    "evolves_to": [{
        "evolves_to": [{
            "evolves_to": [],
            "species": {
                "name": "nidoqueen"
            }
        }],
        "species": {
            "name": "nidorina"
        }
    }],
    "species": {
        "name": "nidoran-f"
    }
}
, traverse = obj => {
  if(!obj.evolves_to.length) {
    console.log(obj.species.name)
    return
  } else {
    console.log(obj.species.name, "=>")
    traverse(obj.evolves_to[0])    
  }
}

traverse(chain)

Or to collect values in an array:

const chain = {
    "evolves_to": [{
        "evolves_to": [{
            "evolves_to": [],
            "species": {
                "name": "nidoqueen"
            }
        }],
        "species": {
            "name": "nidorina"
        }
    }],
    "species": {
        "name": "nidoran-f"
    }
}
, arr = []
, traverse = obj => {
  if(!obj.evolves_to.length) {
    arr.push(obj.species.name)
    return
  } else {
    arr.push(obj.species.name)
    traverse(obj.evolves_to[0])    
  }
}

traverse(chain)
console.log(arr.join(" -> "))

Sign up to request clarification or add additional context in comments.

Comments

0

A simple recursive function should do the trick:

const evolutions = { 
  "chain": {
    "evolves_to": [{
        "evolves_to": [{
            "evolves_to": [],
            "species": {
                "name": "nidoqueen"
            }
        }],
        "species": {
            "name": "nidorina"
        }
    }],
    "species": {
        "name": "nidoran-f"
    }
  }
}

function findNoEvolution(obj, evolutionStages) {
  if (obj.evolves_to.length > 0) {
    for (let i = 0; i < obj.evolves_to.length; i += 1) {
      const found = findNoEvolution(obj.evolves_to[i], evolutionStages);
      if (found) {
        evolutionStages.push(obj.species.name)
        return found;
      }
    }
  }
  evolutionStages.push(obj.species.name)
  return true;
}

const evolutionStages = [];
findNoEvolution(evolutions.chain, evolutionStages);
const evolutionStagesStr = evolutionStages.reverse().join(' -> ');
console.log(evolutionStagesStr);

The result evolution array is then reversed to begin from the 1st evolution via reverse() and joined by arrows via joing(' -> ').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.