如何使用jquery过滤多个重复值?

时间:2013-10-25 07:16:50

标签: javascript jquery jquery-mobile filter

我有一个类似以下类型的json。

{
    "19": {
        "entity_id": "19",
            "type_id": "simple",
            "sku": "Dell laptop",
            "description": "The Inspiron 15R laptop features a 15.6\" screen, color options and up to 3rd Gen Intel® Core™ processors to keep you stylish and connected.",
            "short_description": "The Inspiron 15R is a well-balanced laptop with something for everyone. Unless you need longer battery life, there's no need to spend more.",
            "meta_keyword": "The Inspiron 15R is a well-balanced laptop with something for everyone. Unless you need longer battery life, there's no need to spend more.",
            "name": "dell inspiron 15r",
            "meta_title": "Laptop | Dell | Inspiron15R",
            "meta_description": "The Inspiron 15R is a well-balanced laptop with something for everyone. Unless you need longer battery life, there's no need to spend more.",
            "regular_price_with_tax": 32500,
            "regular_price_without_tax": 32500,
            "category_name": "Laptop",
            "category_id": "19",
            "final_price_with_tax": 31000,
            "final_price_without_tax": 31000,
            "is_saleable": "1",
            "image_url": "http://192.168.18.171/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/h/o/how-to-deal-with-hp-laptop-power-issues.jpg"
    },
        "20": {
        "entity_id": "20",
            "type_id": "simple",
            "sku": "Sony Laptop",
            "description": "Although Sony made computers in the 1980s exclusively for the local (Japan) market, the company withdrew from the computer business around the turn of the decade. Sony's re-entry to the global computer market under the new VAIO brand, began in 1996 with the PCV series of desktops",
            "short_description": "Originally an acronym of Video Audio Integrated Operation, this was amended to Visual Audio Intelligent Organizer in 2008 to celebrate the brand's 10th anniversary",
            "meta_keyword": "Originally an acronym of Video Audio Integrated Operation, this was amended to Visual Audio Intelligent Organizer in 2008 to celebrate the brand's 10th anniversary",
            "name": "Sony VAIO",
            "meta_title": "Laptop | Sony | VAIO",
            "meta_description": "Originally an acronym of Video Audio Integrated Operation, this was amended to Visual Audio Intelligent Organizer in 2008 to celebrate the brand's 10th anniversary",
            "regular_price_with_tax": 34299,
            "regular_price_without_tax": 34299,
            "category_name": "Laptop",
            "category_id": "19",
            "final_price_with_tax": 33000,
            "final_price_without_tax": 33000,
            "is_saleable": "1",
            "image_url": "http://192.168.18.171/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/s/o/sony-vaio-vpcea1bgn-bi-2.jpg"
    },
        "21": {
        "entity_id": "21",
            "type_id": "simple",
            "sku": "HTC Mobiles",
            "description": "HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense™",
            "short_description": "HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense™",
            "meta_keyword": "HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense™",
            "name": "HTC Desire X Dual Sim",
            "meta_title": "SmartPhones | HTC | DESIRE X",
            "meta_description": "HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense™",
            "regular_price_with_tax": 13500,
            "regular_price_without_tax": 13500,
            "category_name": "Smartphones",
            "category_id": "20",
            "final_price_with_tax": 13500,
            "final_price_without_tax": 13500,
            "is_saleable": "1",
            "image_url": "http://192.168.18.171/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/h/t/htx-desire-x-ii.jpg"
    },
        "22": {
        "entity_id": "22",
            "type_id": "simple",
            "sku": "Nokia Mobiles",
            "description": "Award-winning PureView technology, Carl Zeiss optics and six physical lenses. The Nokia Lumia 925 lets you capture the great moments, day or night.",
            "short_description": "Award-winning PureView technology, Carl Zeiss optics and six physical lenses. The Nokia Lumia 925 lets you capture the great moments, day or night.",
            "meta_keyword": "Award-winning PureView technology, Carl Zeiss optics and six physical lenses. The Nokia Lumia 925 lets you capture the great moments, day or night.",
            "name": "Nokia Lumia 925",
            "meta_title": "SmartPhones | Nokia | Lumia 925",
            "meta_description": "Award-winning PureView technology, Carl Zeiss optics and six physical lenses. The Nokia Lumia 925 lets you capture the great moments, day or night.",
            "regular_price_with_tax": 29930,
            "regular_price_without_tax": 29930,
            "category_name": "Smartphones",
            "category_id": "20",
            "final_price_with_tax": 29930,
            "final_price_without_tax": 29930,
            "is_saleable": "1",
            "image_url": "http://192.168.18.171/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/n/o/nokia-lumia-925.jpg"
    }
}

现在我想过滤分类ID和分类名称值的副本然后我想要附加listview我尝试使用以下方法仅过滤类别名称

<script type="text/javascript">
  $(document).on('pageshow', function () {
    $.mobile.loading('show');
    $.getJSON("http://vinoth.com/magento/api/rest/products", function (data) {
        var uniqueCategory = [];
        $.each(data, function (i, row) {
            if ($.inArray(row.category_name, uniqueCategory) === -1) uniqueCategory.push(row.category_name)

        });
        for (var i = 0; i < uniqueCategory.length; i++) {
            $("#category").append('<li><a href="#"><h2>' + uniqueCategory[i] + '</h2></a></li>')
        }
        $("#category").listview('refresh');
        $.mobile.loading('hide');
    });
  });
</script>

如何过滤category_id和category_name?。我想附加id和名称

1 个答案:

答案 0 :(得分:0)

将您的成功回调更改为此

     var dict = {};
    $.each(data, function(i, row) {

    if(!dict.hasOwnProperty(row.category_id;)){
     dict[id] = row.category_name;

     //DO YOUR UI THING HERE
     }

  });