需求:递归遍历arr,给name名称后面加上后缀“---111”,直到children下没有children为止
原数组:
遍历后数组:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>递归遍历children</title> </head> <script src="js/jquery-3.1.1.min.js"></script> <body> </body> <script> let arr = [{ name: 'test1' },{ name: 'test2', children: [{ name: 'test2-1' }] },{ name: 'test3', children: [{ name: 'test1-1', children: [{ name: 'test2-1-1' }] }] }] calleArr(arr); function calleArr(array){ for(i in array){ var data=array[i]; if(data.children){ data.name=data.name+"--111"; calleArr(data.children) //自己调用自己 }else{ data.name=data.name+"--111"; } } } console.log(arr); </script> </html>