2014-07-01 72 views
1

我有点新在这里..我试图学习jquery .. 在jquery中我只想要那些有唯一标题的行(即)行1,2和3只...从json数据中删除具有重复属性的行

我有表的结构是这样的:

在SQL Server中我有这样

QuestionType Title 
Parent1  A 
Parent2  B 
Child1   X 
Child1   X 
Child1   X 

表列,这是我的AJAX功能

$.ajax({ 
    //parameters 
    success: function (response) { 
     result= response.d; 

     if (result.QuestionType == "Child1") { 

      //remove duplicate question.Title 

     //I tried Code like this : if(!$.unique(question.Title)) 
         //{$.Remove()} 
         //this is not working 

     } 

我也试过这个代码:

   var groups = []; 
       $.each(chick, function (i, item) { 
        $.each(item.Title, function (j, group) { 
         if ($.inArray(group, groups) == -1) { 
          groups.push(group); 
         } 
        }); 
       }); 

这也没有工作 我已经看到它在使用删除()和独特()类似的职位。但我没有得到它正确的方式。我我也希望如果有一些lambda表达式可以在JQuery中使用像不同的()function..But没有得到它正确的方式...

任何建议将是有益的

+0

如果可能尝试在服务器端实现返回,而不是返回大块数据的不同的数据和在客户端删除。 – malkam

+0

我不允许它..我需要删除json数据 – user3767164

+0

好吧,所以'结果'将是'QuestionType'和'标题'属性的对象数组? – malkam

回答

1

试试这个

$.ajax({ 
    //parameters 
    success: function (response) { 
     result= response.d; 

var d= {}; 
//array with unique objects 
var u= []; 

$.each(result, function(i, el) { 

    if (!d[el.Title]) { 
     d[el.Title] = true; 
     u.push(el); 
    } 
}); 
} 
}); 
+0

它的工作... thanx ..最短的解决方案 – user3767164

+0

欢迎:) – malkam

-1

jQuery是为DOM操作而设计的,例如唯一的方法只适用于DOM元素。不要试图将其用于一切。

为什么不使用哈希表实现自定义逻辑,按标题统计出现的次数,然后删除重复的数据(或将唯一的数据推送到新数组)。

计数的出现:

var occurrences= {}; 

// Count occurrences 
for(var i = 0; i < result.length; i++){ 
    if(occurrences[result[i].Title]){ 
     occurrences[result[i].Title]++; 
    } 
    else{ 
     occurrences[result[i].Title] = 1; 
    } 
} 

然后从原始数组

// Remove the duplicates from the original array 
for(var i = 0; i < result.length; i++){ 
    if(occurrences[result[i].Title] !== 1){ 
     result.splice(i, 1); 
     i--; 
    } 
} 

除去重复者或者推独特的人到新的数组:

// Push the unique ones to new array 
var unique = []; 
for(var i = 0; i < result.length; i++){ 
    if(occurrences[result[i].Title] === 1){ 
     unique.push(result[i]); 
    } 
} 
0

上下工夫DOM树中,首先必须将主题插入到DOM中,即在<template> ta g将不会呈现。那么这将在您的示例表,如果<table id="filter-table">工作:

var items = []; 

$('#filter-table tr td:nth-child(2)').filter(function(i, item) 
{ 
    var exists = -1 !== items.indexOf(item.textContent); 
    items.push(item.textContent); 
    return exists; 
}).parent().remove(); 

运行例如在JSFiddle(用硬编码DOM)

+0

实际上它是表中的sql ..不在HTML ..从我这边没有清除早期..AI学..我已编辑我的帖子 – user3767164