我确实有这种对象
var j = [{'one':1},{'two':2},{'three':3},{'four':4},{'five':5},{'one':1}];
现在我想跳过重复的记录。任何人都可以建议我的方式吗?
我确实有这种对象
var j = [{'one':1},{'two':2},{'three':3},{'four':4},{'five':5},{'one':1}];
现在我想跳过重复的记录。任何人都可以建议我的方式吗?
过滤具有多个属性的对象的通用解决方案。
var list = [{'one':1},{'two':2},{'four':4},{'one':1},{'four':4},{'three':3},{'four':4},{'one':1},{'five':5},{'one':1}];
Array.prototype.uniqueObjects = function(){
function compare(a, b){
for(var prop in a){
if(a[prop] != b[prop]){
return false;
}
}
return true;
}
return this.filter(function(item, index, list){
for(var i=0; i<index;i++){
if(compare(item,list[i])){
return false;
}
}
return true;
});
}
var unique = list.uniqueObjects();
编辑:
无法比较第一个或第二个属性,因为对象的属性在 javascript 中不按顺序排列。我们可以做的是使用属性进行比较。
Array.prototype.uniqueObjects = function (props) {
function compare(a, b) {
var prop;
if (props) {
for (var j = 0; j < props.length; j++) {
prop = props[j];
if (a[prop] != b[prop]) {
return false;
}
}
} else {
for (prop in a) {
if (a[prop] != b[prop]) {
return false;
}
}
}
return true;
}
return this.filter(function (item, index, list) {
for (var i = 0; i < index; i++) {
if (compare(item, list[i])) {
return false;
}
}
return true;
});
};
var uniqueName = list.uniqueObjects(["name"]);
var uniqueAge = list.uniqueObjects(["age"]);
var uniqueObject = list.uniqueObjects(["name", "age"]);
you could do something like:
var j = [{'one':1},{'two':2},{'three':3},{'four':4},{'five':5},{'one':1}];
var keyarr = [];
var unique = [];
j.map(function(obj, index) {
for(var key in obj ) {
console.log( key );
if( $.inArray(key, keyarr) < 0 ) {
var newobj = {};
newobj[key] = obj[key];
unique.push(newobj)
}
}
});
console.log( unique );
Demo jsfiddle
You can use "map" aka object to keep track of distinct objects. Assuming uniqueness is defined by both the key and value being the same.
var data = [{'one':1},{'two':2},{'three':3},{'four':4},{'five':5},{'one':1}];
var tempMap = {}; // keep track of unique objects with key mapping to the object's key&value
var distinct = []; // resulting list containing only unique objects
var obj = null;
for (var i = 0; i < data.length; i++) {
obj = data[i];
for (var key in obj) { // look in the object eg. {'one':1}
if (obj.hasOwnProperty(key)) {
if (!tempMap.hasOwnProperty(key + obj[key])) { // not in map
tempMap[key + obj[key]] = obj; // then add it to map
distinct.push(obj); // add it to our list of distinct objects
}
break;
}
}
}
console.log(distinct); // list of distinct objects
See fiddle: http://jsfiddle.net/amyamy86/saSqt/
1)通过数组循环。
2) 在该循环中,遍历对象
3) 将键保存在全局变量中
4) 如果键已经存在,则从数组中删除对象。
但是,创建一个对象数组而不是一个大对象有什么意义呢?然后任何重复的键将自动被覆盖......
喜欢
{
"one" : 1,
"two" : 2
...
}
也试试这个:
var j = [{'one':1},{'two':2},{'three':3},{'four':4},{'five':5},{'one':1}],
d = [],
key = [];
d = j.slice(0);
function getKeys(obj) {
var r = []
for (var k in obj) {
if (!obj.hasOwnProperty(k))
continue
r.push(k)
}
return r
}
for(var i=0; i< d.length; i++){
key.push(getKeys(d[i]));
}
for(var kI = 0; kI < key.length; kI++){
for(var kJ = 0; kJ < key.length; kJ++){
if(kI !== kJ && key[kI][0] === key[kJ][0]){
key.splice(kJ,1);
d.splice(kJ,1);
}
}
}
console.log(j);
console.log(d);
查看适用于 JavaScript 的 Linq ( linq.js ),这是 CodePlex 提供的免费 JavaScript 库:
您可以在参考页面上实时试用代码片段(包含在 linqpad 包中,先解压缩 - 然后将代码片段粘贴到代码框中,结果会立即显示):
LinqJS\linq.js_ver2.2.0.2\reference.htm
与 C# LINQ 一样,它有一个.distinct方法,允许您将 JSON 数组作为单线器完成这项工作,例如:
var array = [100, 200, 30, 40, 500, 40, 200];
var ex1 = Enumerable.From(array).Distinct().ToArray(); // [100, 200, 30, 40, 500]
它具有您从 C# LINQ 中了解的大多数方法,并且可以作为非 jQuery 或作为具有完整 NUGET 支持的 jQuery 版本提供。
也支持具有命名属性的数组,例如您在示例中拥有的属性,例如:
var list = [
{ one: 2, two: 4, three: 1 },
{ one: 4, two: 7, three: 5 },
{ one: 2, two: 4, three: 1 },
{ one: 7, two: 3, three: 2 },
];
Enumerable.From(list).Distinct("$.one")
.OrderBy("$.a").ThenBy("$.two").ThenBy("$.three")
.Select("$.one + ':' + $.two + ':' + $.three")
给你结果:
2:4:1
4:7:5
7:3:2