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>