SoFunction
Updated on 2025-04-03

Analysis of JS traversal array and printing array instances

This article analyzes the JS traversal array and printing array methods. Share it for your reference, as follows:

I've been having a headache with JS printing arrays, and I haven't seen any better methods. I've written a simple method to backup.

// traverse arrayvar dd = {
'a' : '111111',
'b' : '222222',
'c' : '333333'
}
dd['d'] = ['444444','55555555'];
// traverse arrayfunction scan_array(arr) {
for(var key in arr) { // This is the keyif(typeof(arr[key]) == 'array' || typeof(arr[key]) == 'object') {// Recursive callscan_array(arr[key]);
} else {
(key + ' = ' + arr[key] + '<br>');
}
}
}
function successionPrint(str,num) {
  num = parseInt(num);
  var return_str = '';
  for (var i = 1; i<=num; i++) {
    return_str +=str;
  }
  return return_str;
}
function __debug(param, flag) {
  if (!param || typeof(param) == 'number' || typeof(param) == 'string') {
      return param;
  }
  var t = typeof(param) + '(\n';
  flag = flag ? parseInt(flag) + 1 : 1;
  for(var key in param) {
    if(typeof(param[key]) == 'array' || typeof(param[key]) == 'object') {
      var t_tmp = key + ' = ' + __debug(param[key],flag);
      t += successionPrint('\t', flag) + t_tmp + '\n';
    } else {
        var t_tmp = key + ' = ' + param[key];
        t += successionPrint('\t', flag) + t_tmp + '\n';
      }
    }
  t = t + successionPrint('\t', flag-1) + ')';
  return t;
}
function _debug(param) {
  alert(__debug(param));
}

For more information about JavaScript arrays, please check out the topic of this site:Summary of JavaScript array operation skills

I hope this article will be helpful to everyone's JavaScript programming.