一.将value为对象数组的map转换成一维对象数组
underscoreTest.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Underscore</title> <script src="lib/underscore/underscore1.6.0.js"></script> </head> <body> </body> </html> <script type="text/javascript"> var map = {'01':[{id:'001',name:'test001'},{id:'003',name:'test003'},{id:'002',name:'test002'}],'02':[{id:'004',name:'test004'},{id:'005',name:'test005'},{id:'008',name:'test008'}]}; console.log(_.flatten(_.toArray(map))); </script>
运行结果:
二.将成员为key-value的数组转换成一维对象数组,其中value又是一个对象数组
underscoreTest02.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Underscore</title> <script src="lib/underscore/underscore1.6.0.js"></script> </head> <body> </body> </html> <script type="text/javascript"> var arr1 = [{name:'car1',age:'29'},{name:'bi',age:'28'}]; var arr2 = [{name:'car01',age:'39'},{name:'bi01',age:'38'}]; var arr3 = [{name:'car02',age:'49'},{name:'bi02',age:'48'}]; var arrMap = []; arrMap['arr1'] = arr1; arrMap['arr2'] = arr2; arrMap['arr3'] = arr3; console.log(arrMap); console.log(_.flatten(_.values(arrMap))); </script>
运行结果:
三.filter及each的使用
underscoreTest03.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Underscore</title> <script src="lib/underscore/underscore1.6.0.js"></script> </head> <body> </body> </html> <script type="text/javascript"> var jsonArr = [{'isChecked': true,'value':'01'},{'isChecked': false, 'value':'03'},{'isChecked': true, 'value':'02'}]; var afterFilter = _.filter(jsonArr, function(a) { return a.isChecked; }); _.each(afterFilter, function(item) { document.write(item.isChecked + ' ' + item.value + '<br/>'); }); </script>
运行结果:
四.each与flatten的综合使用
underscoreTest04.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Underscore</title> <script src="underscore-1.5.1.min.js"></script> </head> <body> </body> </html> <script type="text/javascript"> var jsonArr = [{main:{num:'1111'}},{relation:[{num:'2222'},{num:'333'}]}]; var res = []; _.each(jsonArr, function(obj) { var t = _.values(obj); res = res.concat(t); }); res = _.flatten(res); console.log(res); </script>
运行结果:
附:在js代码中加debugger关键字,调试模式下就会在debugger处暂停
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Underscore</title> <script src="lib/underscore/underscore1.6.0.js"></script> </head> <body> </body> </html> <script type="text/javascript"> debugger; var jsonArr = [{'isChecked': true,'value':'01'},{'isChecked': false, 'value':'03'},{'isChecked': true, 'value':'02'}]; var afterFilter = _.filter(jsonArr, function(a) { return a.isChecked; }); debugger; _.each(afterFilter, function(item) { document.write(item.isChecked + ' ' + item.value + '<br/>'); }); </script>
运行效果:打开首先在第一个debugger处停留,F8后就会在第二个debugger处停留。
相关推荐
Explore the Underscore.js library by example using a test-driven development approach About This Book Understand and learn to apply functional programming principles using the built-in functions of ...
Explore the Underscore.js library by example using a test-driven development approach About This Book Understand and learn to apply functional programming principles using the built-in functions of ...
Underscore.js是目前最好用的Javascript工具包,提供了100多个函数。Underscore.php是Underscore.js的PHP移植版,并且实现了部分Underscore.js中的功能! 我花了一个月的时间将Underscore.php的文档翻译为中文,开源...
underscore.js的开发文档,需要使用的朋友们可以看看
underscore.js underscore.js underscore.js
underscore-min.js
underscore源码学习计划
Underscore1.8.3 参考手册.CHM,Underscore中文教程,Underscore离线手册
underscore js工具类,underscore为集合类对象提供了一致的接口
underscore源码解析
underscore-1.6.0.zip
underscore-1.4.3.zip
underscore.js api文档 chm
1.2.3的Underscore中文文档,采用网页版压缩制成的chm文件,方便查看,方便点击链接
underscore javascript库 使用教程.zip
使用第三方模块Underscore.js,Immutable.js,UUID(源代码+截图)使用第三方模块Underscore.js,Immutable.js,UUID(源代码+截图)使用第三方模块Underscore.js,Immutable.js,UUID(源代码+截图)使用第三方模块...
underscore.js源码,适合初始化学习javascript,强烈推荐
underscore1.5.2中文帮助,Underscore 是一个JavaScript实用库,提供了类似Prototype.js (或 Ruby)的一些功能,但是没有继承任何JavaScript内置对象。它弥补了部分jQuery没有实现的功能,同时又是Backbone.js必不可少的...
backbone附属包underscore使用