我需要过滤包含重复标题的对象数组,但描述是唯一的。 例如
[
{
"Title": "New York",
"Description": "A healthy and modernized transit system"
},
{
"Title": "New York",
"Description": "changed transit system"
},
{
"Title": "New York",
"Description": "xyz"
},
{
"Title": "New York",
"Description": "abc"
},
{
"Title": "chicago",
"Description": "jdfjjfj"
},
{
"Title": "chicago",
"Description": "abcdfdjf"
}
]
正如你所看到的,标题是重复的,而它的描述是唯一的。所以任何人都可以告诉我如何过滤这个过滤掉唯一标题和描述唯一的对象数组。
基本上,过滤应该是标题首先带有以下唯一描述。
答案 0 :(得分:2)
var rs = {};
$.each(objs, function(i, obj) {
if (rs[obj.Title] === undefined) rs[obj.Title] = [];
rs[obj.Title].push(obj.Description);
});
在jsFiddle上检查:http://jsfiddle.net/U6qu4/
答案 1 :(得分:0)
for(var i=0;i<=objs.length;i++){
for(var j=i+1;j<=objs.length;j++){
if(objs[i].Title==objs[j].Title || objs[i].Description==objs[j].Description){
//do some stuff to filter
objs.splice(j,1) //it can be used to remove the matched element
}
}