我试图接受一个JSON对象,遍历它,删除重复和冗余数据并按日期顺序输出(日期戳记在JSON对象中)。
具体清单如下:
这必须在纯JavaScript中完成。这是我到目前为止的代码:
public class Message
{
public static readonly int SizeInBytes = Marshal.SizeOf(typeof(this_type));
}
我能够通过console.log()看到对象,但每当我尝试操作它们时,我一直看到输出时出现错误和未定义。我不清楚这样做的确切机制。
非常感谢,我在我的智慧结束。
好的,为了扩展这个,我在JSFIDDLE上设置了一个帐户 Second one
答案 0 :(得分:1)
我已经分享了你的jsfiddle:http://jsfiddle.net/limowankenobi/pak34wrz/
它仍然需要更多的工作,因为我只是删除重复项而不考虑日期。您应该能够根据您的要求对其进行修改以使用日期。
我创建了两个小函数:
第一个按照数组元素的属性(参数data
)对数组(参数column
)进行分组。
结果将是一个对象(地图),其属性(键)是所选列的唯一值,每个值都是具有该键的记录列表。
function groupBy(column, data) {
var groups = {};
data.forEach(function (itm) {
groups[itm[column]] = groups[itm[column]] || [];
groups[itm[column]].push(itm);
});
return groups;
}
第二个功能是展平列表地图。它需要一个对象迭代对象的每个属性(键)并选择一个元素。在这种情况下,我选择第一个元素,但我相信你应该修改它以根据日期选择元素。
function uniquify(groups) {
var unique = [];
for (var key in groups) {
if (groups.hasOwnProperty(key)) {
unique.push(groups[key][0]);
}
}
return unique;
}
通过这种方式,执行所需操作的功能将类似于:
function arrUnique(arr) {
var groupsById = groupBy("_id", arr);
var uniqueIds = uniquify(groupsById);
var groupsByEmail = groupBy("email", uniqueIds);
return uniquify(groupsByEmail);
}
例如,groupBy("_id", arr)
的结果是
{
"jkj238238jdsnfsj23": [
{
"_id": "jkj238238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:30:20+00:00"
},
{
"_id": "jkj238238jdsnfsj23",
"email": "coo@bar.com",
"firstName": "Ted",
"lastName": "Jones",
"address": "456 Neat St",
"entryDate": "2014-05-07T17:32:20+00:00"
},
{
"_id": "jkj238238jdsnfsj23",
"email": "bill@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "888 Mayberry St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
],
"edu45238jdsnfsj23": [
{
"_id": "edu45238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Ted",
"lastName": "Masters",
"address": "44 North Hampton St",
"entryDate": "2014-05-07T17:31:20+00:00"
}
],
"wabaj238238jdsnfsj23": [
{
"_id": "wabaj238238jdsnfsj23",
"email": "bog@bar.com",
"firstName": "Fran",
"lastName": "Jones",
"address": "8803 Dark St",
"entryDate": "2014-05-07T17:31:20+00:00"
}
],
"sel045238jdsnfsj23": [
{
"_id": "sel045238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:32:20+00:00"
}
],
"qest38238jdsnfsj23": [
{
"_id": "qest38238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:32:20+00:00"
}
],
"vug789238jdsnfsj23": [
{
"_id": "vug789238jdsnfsj23",
"email": "foo1@bar.com",
"firstName": "Blake",
"lastName": "Douglas",
"address": "123 Reach St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
],
"wuj08238jdsnfsj23": [
{
"_id": "wuj08238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "Micah",
"lastName": "Valmer",
"address": "123 Street St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
],
"belr28238jdsnfsj23": [
{
"_id": "belr28238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Tallulah",
"lastName": "Smith",
"address": "123 Water St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
]
}
然后通过_id调用uniqify
分组结果:
[
{
"_id": "jkj238238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:30:20+00:00"
},
{
"_id": "edu45238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Ted",
"lastName": "Masters",
"address": "44 North Hampton St",
"entryDate": "2014-05-07T17:31:20+00:00"
},
{
"_id": "wabaj238238jdsnfsj23",
"email": "bog@bar.com",
"firstName": "Fran",
"lastName": "Jones",
"address": "8803 Dark St",
"entryDate": "2014-05-07T17:31:20+00:00"
},
{
"_id": "sel045238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:32:20+00:00"
},
{
"_id": "qest38238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:32:20+00:00"
},
{
"_id": "vug789238jdsnfsj23",
"email": "foo1@bar.com",
"firstName": "Blake",
"lastName": "Douglas",
"address": "123 Reach St",
"entryDate": "2014-05-07T17:33:20+00:00"
},
{
"_id": "wuj08238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "Micah",
"lastName": "Valmer",
"address": "123 Street St",
"entryDate": "2014-05-07T17:33:20+00:00"
},
{
"_id": "belr28238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Tallulah",
"lastName": "Smith",
"address": "123 Water St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
]
如果我们现在通过电子邮件对此结果进行分组,我们会得到:
{
"foo@bar.com": [
{
"_id": "jkj238238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:30:20+00:00"
},
{
"_id": "sel045238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:32:20+00:00"
},
{
"_id": "qest38238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:32:20+00:00"
},
{
"_id": "wuj08238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "Micah",
"lastName": "Valmer",
"address": "123 Street St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
],
"mae@bar.com": [
{
"_id": "edu45238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Ted",
"lastName": "Masters",
"address": "44 North Hampton St",
"entryDate": "2014-05-07T17:31:20+00:00"
},
{
"_id": "belr28238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Tallulah",
"lastName": "Smith",
"address": "123 Water St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
],
"bog@bar.com": [
{
"_id": "wabaj238238jdsnfsj23",
"email": "bog@bar.com",
"firstName": "Fran",
"lastName": "Jones",
"address": "8803 Dark St",
"entryDate": "2014-05-07T17:31:20+00:00"
}
],
"foo1@bar.com": [
{
"_id": "vug789238jdsnfsj23",
"email": "foo1@bar.com",
"firstName": "Blake",
"lastName": "Douglas",
"address": "123 Reach St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
]
}
最后在uniqify
上应用此结果:
[
{
"_id": "jkj238238jdsnfsj23",
"email": "foo@bar.com",
"firstName": "John",
"lastName": "Smith",
"address": "123 Street St",
"entryDate": "2014-05-07T17:30:20+00:00"
},
{
"_id": "edu45238jdsnfsj23",
"email": "mae@bar.com",
"firstName": "Ted",
"lastName": "Masters",
"address": "44 North Hampton St",
"entryDate": "2014-05-07T17:31:20+00:00"
},
{
"_id": "wabaj238238jdsnfsj23",
"email": "bog@bar.com",
"firstName": "Fran",
"lastName": "Jones",
"address": "8803 Dark St",
"entryDate": "2014-05-07T17:31:20+00:00"
},
{
"_id": "vug789238jdsnfsj23",
"email": "foo1@bar.com",
"firstName": "Blake",
"lastName": "Douglas",
"address": "123 Reach St",
"entryDate": "2014-05-07T17:33:20+00:00"
}
]