Javascript脚本

www.superstr.work

Superstr-1.0.js

function Superstr(){}
Superstr.prototype.version='1.0';
Superstr.prototype.arrays={
   /**
	*移除数组中重复的对象
	*param:arr 数组
	*param:property 数组中对象的属性名称
	*return:array 处理后的数组
	*/
	only: function(arr, property) {
		let hash = {}
		var array = arr.reduce((item, next) => {
		    if (!hash[next[property]]) {
				hash[next[property]] = true;
				item.push(next);
		    }
			return item;
		}, [])
		return array;
	},
   /**
	*过滤数组中的对象
	*param:arr 数组
	*param:property 数组中对象的属性名称
	*param:value 数组中对象属性的值
	*return:array 处理后的数组
	*/
	remove: function(arr,property,value) {
		var array = arr.filter(
			(obj) => {return obj[property] != value} 
		)
		return array;
	}
}
var _ = new Superstr();

index.html

<html>
	<head>
		<script src="Superstr-1.0.js"></script>
		<script>
			var superstr = new Superstr();
			console.log(_.version);//1.0
			console.log(superstr.version);//1.0
			console.log(_ instanceof Object);//true
			console.log(_ instanceof Superstr);//true
			
			var arr = [{'id':1,'name':'a'}, {'id':1,'name':'a'}, {'id':2,'name':'a'}, {'id':2,'name':'A'}, {'id':3,'name':'B'}, {'id':3,'name':'b'}, {'id':4,'name':'b'}]
			console.log(_.arrays.only(arr, 'id'));
			console.log(_.arrays.only(arr, 'name'));
			console.log(_.arrays.remove(arr, 'id', 1));
			console.log(_.arrays.remove(arr, 'name', 'a'));
		</script>
	</head>
	<body>
		流行浏览器:Google Chrome、Mozilla Firefox<br/>
		浏览器内核:Webkit、Blink、Gecko
	</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值