5

我需要过滤包含重复标题但描述是唯一的对象数组。例如

[
    {
        "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"
    }
]

正如你所看到的,标题是重复的,而它的描述是唯一的。所以任何人都可以告诉我如何过滤这个对象数组,它过滤掉唯一的标题和描述是唯一的。

基本上过滤应该是这样的,即标题首先带有以下独特的描述。

4

2 回答 2

2
var rs = {};
$.each(objs, function(i, obj) {
  if (rs[obj.Title] === undefined) rs[obj.Title] = [];
  rs[obj.Title].push(obj.Description);
});

Check it on jsFiddle: http://jsfiddle.net/U6qu4/

于 2013-07-24T12:52:16.040 回答
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
    }
}

于 2013-07-24T13:24:45.270 回答