K
K
Kim_Soal2018-02-06 16:37:34
JavaScript
Kim_Soal, 2018-02-06 16:37:34

js return value problem?

there is a function code

function Name(){
 ........
 var ept = getArrayFromTree(path.split("/"), null);
 console.log("1", ept);
}

and there is that called function
function getArrayFromTree(step_to, now_arr){
 ......
 else{
  console.log("2",now_arr);
  return now_arr;
 }
}

when the function is called, it gets to the console
2 {выводится то что нужно}
1 undefined

Full code
<script type="text/javascript">

var tree = document.getElementById("qwe").innerHTML;
tree = JSON.parse(tree);
var new_images = document.getElementById("new_images");



generetaImagesFolders("");


function generetaImagesFolders(path){

  var now = document.getElementById("new_images_path_now").innerHTML;
  var step_to = now+path;
  document.getElementById("new_images_path_now").innerHTML = step_to;
  new_images.innerHTML = "";


  var ept = getArrayFromTree(path.split("/"), null);
  
  console.log("1", ept);
  

  for( key in ept){
    
    new_images.innerHTML += "<div data-name-path='"+key+"'>"+key+"</div>";
  }

  renewEvents();
}
  
function renewEvents(){
  var sometings = new_images.querySelectorAll("div");
  for(var i = 0; i<sometings.length; i++){
    sometings[i].onclick = function(){
      // console.log(this.getAttribute('data-name-path'));
      generetaImagesFolders(this.getAttribute('data-name-path'));
    }
  }
}


function getArrayFromTree(step_to, now_arr){
  

  if(now_arr === null) {
    now_arr = tree;
  }

  var first = step_to.shift();
  

  if(first !== "" && first !== undefined) {
    getArrayFromTree(step_to, now_arr[first]);
  }

  else{
    console.log("2",now_arr);
    return now_arr;
  }



}


</script>


at the same time, the first time everything is fine

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Zhivagin, 2018-02-06
@Krasnodar_etc

Maybe it just doesn't return there? Can I have the full code for the second function?

A
Alexander Manakov, 2018-02-06
@gogolor

I didn’t look much, but offhand try adding return
return getArrayFromTree(step_to, now_arr[first]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question