underscore.js框架

另一种简化js操作方式
想用的但是js步骤繁琐的
比如:
js求数组最值:Math.max.apply(null,数组对象)
u.js求数组最值:_.max(数组对象);
api地址

常用方法

underscore.js
(1)快速遍历:_.each(对象/数组,function(ele每次遍历的元素,index元素的下标或键名,list元素所在的集合){})
(2)求最值:_.max(数组);
(3)随机数:_.random(min,max); 返回min到max之间的随机整数;
		 _.random(max);  返回0到max之间的随机整数;
(4)返回删除指定元素后的数组:_.without(数组对象,删除值1,删除值2,...); 删除值只能是数值,不能是数组
(5)随机返回数组中的元素:_.simple(数组对象,num/可选表示随机返回个数,不写默认返回一个);

代码示例:

<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<script src='js/underscore.js'></script>

	<style>
		.canvas{
			border:solid 2px black;
		}
	
	
	</style>
</head>
<body>

	<canvas width="300px" height="200px" class='canvas'>您的浏览器不支持canvas标签,请变更支持canvas的浏览器</canvas>
	<script>
			var	canvas=document.querySelector(".canvas");
			var bi=canvas.getContext('2d');

			var arr=['a','b','c'];
			var narr=[1,2,3,4];
			var obj={
				name:'jeff',
				age:18,
				gender:'male'
			}
			_.each(obj,function(ele,index,list){
				console.log('---------------')
				console.log(ele);
				console.log(index);
				console.log(list);
			})

			console.log(_.max(narr))
			console.log(_.random(22));
			console.log(_.random(22,30));

			console.log(_.without(narr,[1,2]));
			console.log(_.without(arr,'a'));

	</script>
	
</body>

</html>
发布了281 篇原创文章 · 获赞 3 · 访问量 4827

猜你喜欢

转载自blog.csdn.net/weixin_43294560/article/details/103990261
今日推荐