js 遍历 object 属性和值的方法
来源:互联网 发布:哪些网游国服有mac版 编辑:程序博客网 时间:2024/05/21 17:26
国外网站上 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:20function 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 well if ( 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 well if ( level == 0 ) { str += ''; } return str; }
来自:http://stackoverflow.com/questions/957537/how-can-i-print-a-javascript-object
0 0
- js 遍历 object 属性和值的方法
- js中遍历对象的属性和值的方法
- JS遍历属性和方法
- JS遍历属性和方法
- 我的JS笔记---Object的属性和方法
- JS的Object类的属性和方法
- JS的Object类的属性和方法
- JS的Object类的属性和方法
- JS的Object类的属性和方法
- js中通用的Object属性和方法
- JS基础之object所具有的属性和方法
- JS实现打印对象[object]的所有方法和属性
- js 遍历对象的属性和值
- js遍历Object所有属性
- js中Object常用方法和属性
- js中Object常用方法和属性
- js中Object常用方法和属性
- 如何遍历一个JS对象的属性和方法
- 关于面向对象的的设计原则的使用
- LeeCode---Add Two Numbers
- C++ Primer再读笔记-1
- Cocos2d-x优化中多线程并发访问
- HDU 5095 Linearization of the kernel functions in SVM(模拟, 水题)
- js 遍历 object 属性和值的方法
- 第11周 项目6-5 输出1000以内的所有回文数
- 数据结构与算法学习之路:二分查找的非递归和递归算法
- HDU 5090 Game with Pearls(贪心, 二部图最大匹配)
- Servlet JSP 二重修炼:Filter过滤器
- CC2540 DMA学习
- 设计模式原则详解
- 在处理URL时对中文的转码过程
- win7 java环境变量设置