从获得使用jQuery JSON数组的独特成果 [英] Get unique results from JSON array using jQuery

查看:109
本文介绍了从获得使用jQuery JSON数组的独特成果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code此块显示从我的数组中的类别变成了jQuery开发的简单列表。

它工作正常,但如果有从类别篮球3项,该类别将出现3次。

我怎么能做出这样他们只能出现一次?谢谢你。

这里的code:

 函数loadCategories(){
    console.debug(关于使用排序类型刷新:'+ sortType);
    变种项= [];    $。每个(catalog.products,
        功能(指数值){
            items.push('<李ID =+指数+'>'+
                  '<数据身份=产品HREF =./ productList.page?category='+ value.category +'>' +
                  '< p =风格保证金底:0像素;的margin-top:0像素;>' + value.category +'&下; / P>&下; / A> < /李>');
        }
    );    categoryView.html(items.join(''));
    categoryView.listview('刷新');
}

这里的code为我的数组:

  VAR目录= {产品:[
{ID:10001,
名:山地自行车,
色:灰色/黑色
长说明:12速,碳山地车。
说明:,
大小:20英寸
类别:户外/设备租赁
运动:骑自行车,
品牌:达芬奇
最佳销售员: },{ID:10002,
名:临多重篮球
色:彩虹,
长说明:发售,本周仅此限量版篮球有多种颜色,并提供亲性能乐趣和在球场上的游戏!。!
说明:限量版篮球。
大小:N / A,
类别:团队档,
运动:篮球,
品牌:耐克,
最畅销:X},


解决方案

我不知道你的产品阵列建成(所以我的例子可能需要一些修改根据您的需要),但你可以写一个小片code的,将首先收集你的独特的类别到一个新的数组:

  VAR类别= [];$。每个(catalog.products,功能(指数值){
    如果($ .inArray(value.category,类别)=== -1){
        categories.push(value.category);
    }
});

的jsfiddle演示

类别将持有你所需要的独特的类别,你可以用它来建立你的HTML从(注意与里的ID 元素不过,请记住,ID不能重复的,你可以给他们一点preFIX)。

I have this block of code that displays the "categories" from my array into a JQuery simple list.

It works fine, but if there are 3 items from the category "basketball", the category will appear 3 times.

How could I make it so they only appear once ? Thank you.

Here's the code :

function loadCategories() {
    console.debug('About to refresh with sort type : ' + sortType);
    var items = [];

    $.each(catalog.products,
        function(index, value) {
            items.push('<li id="' + index + '">' +
                  '<a data-identity="productId"  href="./productList.page?category=' + value.category + '" >' +
                  '<p style="margin-bottom:0px;margin-top:0px;">' + value.category + '</p></a> </li>');
        }
    );

    categoryView.html(items.join(''));
    categoryView.listview('refresh');
}

Here's the code for my array :

var catalog = {"products": [
{"id": "10001",
"name": "Mountain bike",   
"color": "Grey/Black",
"long-desc": "12-speed, carbon mountain bike.",
"description": "",
"size": "20 inches",
"category": "Outdoors/ Equipment rental",
"sport": "Cycling",
"brand": "DaVinci",
"top-seller": ""},

{"id": "10002",
"name": "Pro Multi Basketball",   
"color": "Rainbow",
"long-desc": "On sale this week only! This limited edition basketball is multi-coloured, and offers pro performance. Fun and games on the court!",
"description": "Limited edition basketball.",
"size": "N/A",
"category": "Team gear",
"sport": "Basketball",
"brand": "Nike",
"top-seller": "x"},

解决方案

I don't know how your products array is built up (so my example might need some modification according to your needs), but you could write a little piece of code that will first collect your unique categories into a new array:

var categories = [];

$.each(catalog.products, function(index, value) {
    if ($.inArray(value.category, categories) === -1) {
        categories.push(value.category);
    }
});

jsFiddle Demo

categories will hold the unique categories you need, you can use it to build your HTML from that (be careful with the IDs of those li elements though, remember that IDs cannot be duplicate, you might give them a little prefix).

这篇关于从获得使用jQuery JSON数组的独特成果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆