Home > Web Front-end > JS Tutorial > JS traversing arrays and printing array example analysis_javascript skills

JS traversing arrays and printing array example analysis_javascript skills

WBOY
Release: 2016-05-16 15:18:59
Original
1594 people have browsed it

This article analyzes the method of traversing arrays and printing arrays in JS. Share it with everyone for your reference, the details are as follows:

I have always had a headache with JS printing arrays, and I haven’t seen any better method, so I wrote a simple method as a backup.

// 遍历数组
var dd = {
'a' : '111111',
'b' : '222222',
'c' : '333333'
}
dd['d'] = ['444444','55555555'];
// 遍历数组
function scan_array(arr) {
for(var key in arr) { // 这个是关键
if(typeof(arr[key]) == 'array' || typeof(arr[key]) == 'object') {// 递归调用
scan_array(arr[key]);
} else {
document.write(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 &#63; 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));
}

Copy after login

Readers who are interested in more content related to JavaScript arrays can check out the special topic on this site: "Summary of JavaScript array operation skills"

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template