Implementation method of getting value value by recursively traversing objects

  • 2021-06-28 10:37:19
  • OfStack

1 Recursion is generally used to determine whether the object is the same as the parent type

This demonstrates simple object recursion, as well as array recursion.


var obj = { a:{w:1,y:2,x:3},
      b:{s:4,j:5,x:6},
       c:{car:7,cat:8,mao:9}
}
function f(s){
 for(var i in s){
  if(typeof s[i]=="object"){
   f(s[i])
  }else{
 * console.log(s[i]);
 * }
 }
}
f(obj);

Return results: 1,2,3,4,5,6,7,8,9


Related articles: