从json数据中删除具有重复属性的行

时间:2014-07-01 16:50:08

标签: jquery .net visual-studio-2010

我有点新来了..我正在努力学习Jquery .. 在Jquery中。我只想要那些具有唯一Title ..(即)行1,2和3的行...

我有这样的表结构:

在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);
                        }
                    });
                });

这也行不通 我已经看到类似的帖子正在使用remove()和unique()..但我没有正确的方式..我也在寻找是否有一些lambda表达式可以在JQuery中使用,如distinct()函数..但是没有正确的方式......

任何建议都会有帮助

2 个答案:

答案 0 :(得分: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);
    }
});
}
});

答案 1 :(得分:0)

要处理DOM树,首先必须将主题插入DOM,即在<template>标记中不会被渲染。如果<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)