Распечатать многомерный массив в Javascript

  1. function print_r(arr, level) {
  2. var print_red_text = "";
  3. if(!level) level = 0;
  4. var level_padding = "";
  5. for(var j=0; j<level+1; j++) level_padding += " ";
  6. if(typeof(arr) == 'object') {
  7. for(var item in arr) {
  8. var value = arr[item];
  9. if(typeof(value) == 'object') {
  10. print_red_text += level_padding + "'" + item + "' :\n";
  11. print_red_text += print_r(value,level+1);
  12. }
  13. else
  14. print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  15. }
  16. }
  17.  
  18. else print_red_text = "===>"+arr+"<===("+typeof(arr)+")";
  19. return print_red_text;
  20. }
Russian
Категория: 
The code is not tested and may not work
Мультитег: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image