js 遍历 object 属性和值的方法
2014-11-11 20:50
441 查看
国外网站上 js 遍历 object 属性和值的方法
function( o, maxLevel, level ) { if ( typeof level == "undefined" ) { level = 0; } if ( typeof level == "undefined" ) { maxLevel = 0; } var str = ''; // Remove this if you don't want the pre tag, but make sure to remove // the close pre tag on the bottom as well if ( level == 0 ) { str = '<pre>'; } var levelStr = ''; for ( var x = 0; x < level; x++ ) { levelStr += ' '; } if ( maxLevel != 0 && level >= maxLevel ) { str += levelStr + '...</br>'; return str; } for ( var p in o ) { if ( typeof o[p] == 'string' ) { str += levelStr + p + ': ' + o[p] + ' </br>'; } else { str += levelStr + p + ': { </br>' + print( o[p], maxLevel, level + 1 ) + levelStr + '}</br>'; } } // Remove this if you don't want the pre tag, but make sure to remove // the open pre tag on the top as well if ( level == 0 ) { str += '</pre>'; } return str; };
</pre><pre code_snippet_id="515740" snippet_file_name="blog_20141111_2_4562501" name="code" class="html">20:48:20 烟飞烟散 2014/11/11 20:48:20 function printAllObject( o, maxLevel, level ) { if ( typeof level == "undefined" ) { level = 0; } if ( typeof level == "undefined" ) { maxLevel = 0; } var str = ''; // Remove this if you don't want the pre tag, but make sure to remove // the close pre tag on the bottom as well if ( level == 0 ) { str = ''; } var levelStr = ''; for ( var x = 0; x < level; x++ ) { levelStr += ' '; } if ( maxLevel != 0 && level >= maxLevel ) { str += levelStr + '...'; return str; } for ( var p in o ) { if ( typeof o[p] == 'string' ) { str += levelStr + p + ':' + o[p] + ''; } else { str += levelStr + p + ': { ' + printAllObject( o[p], maxLevel, level + 1 ) + levelStr + '}'; } } // Remove this if you don't want the pre tag, but make sure to remove // the open pre tag on the top as well if ( level == 0 ) { str += ''; } return str; };
<pre name="code" class="javascript">function printAllObject( o, maxLevel, level ) {if ( typeof level == "undefined" ) {level = 0;}if ( typeof level == "undefined" ) {maxLevel = 0;}var str = '';// Remove this if you don't want the pre tag, but make sure to remove// the close pre tag on the bottom as wellif ( level == 0 ) {str = '';}var levelStr = '';for ( var x = 0; x < level; x++ ) {levelStr += ' ';}if ( maxLevel != 0 && level >= maxLevel ) {str += levelStr + '\n';return str;}for ( var p in o ) {if ( typeof o[p] == 'string' ) {str += levelStr +p + ': ' + o[p] + ' \n';} else {str += levelStr +p + ': { </br>' + print( o[p], maxLevel, level + 1 ) + levelStr + '}\n';}}// Remove this if you don't want the pre tag, but make sure to remove// the open pre tag on the top as wellif ( level == 0 ) {str += '';}return str;}来自:http://stackoverflow.com/questions/957537/how-can-i-print-a-javascript-object
相关文章推荐
- js中遍历对象的属性和值的方法
- js中遍历对象的属性和值的方法
- js 获取函数的所有参数和遍历JavaScript某个对象所有的属性名称和值的方法
- js 获取函数的所有参数和遍历JavaScript某个对象所有的属性名称和值的方法
- js 获取函数的所有参数和遍历JavaScript某个对象所有的属性名称和值的方法
- IE8对JS的数组,采用属性遍历的方法,解析不一样的地方:
- js遍历对象属性和值
- JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- JS面向对象定义多个属性--object.defineProperties()方法
- js中遍历对象的属性和值
- JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- js遍历子节点子元素附属性及方法
- 遍历js中对象的属性和值
- JS基础之object所具有的属性和方法
- js遍历Object所有属性
- JS遍历属性和方法
- 【JS--基础--对象】--基础用法(通用的Object属性和方法)
- IE8对JS的数组,采用属性遍历的方法,解析不一样的地方:
- JS实现打印对象[object]的所有方法和属性
- js object 对象 属性和方法的使用