如何计算重复的数组元素? (JavaScript的) [英] How to count duplicate array elements? (javascript)

查看:64
本文介绍了如何计算重复的数组元素? (JavaScript的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript,XML和jQuery创建基于XML的菜单。我已成功获取菜单类别,但无法生成类别中的项目。

I'm trying to make an XML based menu with JavaScript, XML and jQuery. I've been successful at getting the categories of the menu, but haven't been able to generate the items in the categories.

我的脚本如下,后来在这个帖子中,我已经询问了这段代码的建议:

My script is as follows, and later in this thread, I've asked for suggestions for this code:

var animalsXMLurl = 'http://dl.dropboxusercontent.com/u/27854284/Stuff/Online/XML_animals.xml';
$(function() {
    $.ajax({
        url: animalsXMLurl, // name of file you want to parse
        dataType: "xml",
        success: function parse(xmlResponse) {

            var data = $("item", xmlResponse).map(function() {
                return {
                    id: $("animal_id", this).text(),
                    title: $("animal_title", this).text(),
                    url: $("animal_url", this).text(),
                    category: $("animal_category", this).text().split('/'),
                };
            }).get();
            var first_item = category_gen(data, 0);
            $('ul.w-nav-list.level_2').append(first_item);
            var categnumber = new Array();
            for (i = 1; i <= data.length; i++) //for splitting id, and getting 0 for category_number (1 or 2 or 3...and so on)
            {
                categnumber[i] = data[i].id.split('_');
                console.log(categnumber[i][0]);
                for (j = 1; j <= data.length; j++) //appending via a function.
                {
                    var data_text = category_or_animal(data, categnumber, j);
                    console.log(data_text);
                    $('ul.w-nav-list.level_2').append(data_text);
                }
            }

            function category_or_animal(d, catg, k) {
                var catg1 = new Array();
                var catg2 = new Array();
                var catg1 = d[k].id.split('_');
                if (d[k - 1]) {
                    var catg2 = d[k - 1].id.split('_');
                    //if(d[k-1].id)
                    if (catg1[0] != catg2[0])
                        return category_gen(d, k);
                } else
                    return '</ul>' + animal_gen(d, k);
            }
            function category_gen(d, z) {
                var category_var = '<li class="w-nav-item level_2 has_sublevel"><a class="w-nav-anchor level_2" href="javascript:void(0);"><span class="w-nav-title">' + d[z].category + '</span><span class="w-nav-arrow"></span></a><ul class="w-nav-list level_3">';
                return category_var;
            }

            function animal_gen(d, z) {
                var animal_var = '<li class="w-nav-item level_3"><a class="w-nav-anchor level_3" href="animals/' + d[z].url + '"><span class="w-nav-title">' + d[z].title + '</span><span class="w-nav-arrow"></span></a></li>';
                return animal_var;
            }

        }, error: function() {
            console.log('Error: Animals info xml could not be loaded.');
        }
    });
});

这是上面代码的JSFiddle链接: http://jsfiddle.net/mohitk117/d7XmQ/4/

Here's the JSFiddle link for the above code: http://jsfiddle.net/mohitk117/d7XmQ/4/

在上面代码我需要一些改动,我认为代码可能有用,所以我要求提出建议:

In the above code I need some alterations, with which I think the code might work, so I'm asking for suggestions:

这是用函数调用单独函数生成的函数上面代码中的菜单:

Here's the function that's calling separate functions with arguments to generate the menu in above code:

function category_or_animal(d, catg, k) {
    var catg1 = new Array();
    var catg2 = new Array();
    var catg1 = d[k].id.split('_');
    if (d[k - 1]) {
        var catg2 = d[k - 1].id.split('_');
        //if(d[k-1].id)
        if (catg1[0] != catg2[0])
            return category_gen(d, k);
    } else
        return animal_gen(d, k) + '</ul>';
}

if(catg1 [0]!= catg2 [0])它检查分裂字符串1_2或1_3是否分别等于1_1或1_2。通过拆分,我的意思是第一个元素:1 ....如果你看一下xml:[ :: Animals XML :: ],你会看到animal_id的格式为%category_number% _ %item_number% ...所以我需要用CATEGORY> ITEM(item =动物名称)创建菜单

At the if(catg1[0] != catg2[0]) it checks if the split string 1_2 or 1_3 is equal to 1_1 or 1_2 respectively. By split, I mean the first element: 1 .... if you have a look at the xml: [ :: Animals XML :: ], you'll see that the animal_id is in the format of %category_number% _ %item_number% ... So I need to create the menu with CATEGORY > ITEM (item=animal name)

现在如果我可以返回category_gen()+ animal() with animal(){在所有匹配的类别ID号的for循环中}那么也许这可能是完整的!但我没有用于调节for循环的计数脚本(i = 0; i< = count(); i ++) ...

Now if I could return category_gen() + animal() with animal(){ in a for loop for all the matching category id numbers} then maybe this could be complete! But I don't of a count script for conditioning the for loop (i=0;i<=count();i++)...

有人知道如何使这个脚本运作吗?

Would anyone know of how to get this script functioning?

推荐答案

很难说是什么如果JSFiddle试图这样做。

Hard to tell what the provided JSFiddle is trying to do.

这是我最好的尝试。我使用JQuery将XML解析为类别并生成项目列表。

This is my best stab at it. I used JQuery to parse the XML out into categories and generate lists of items.

http://jsfiddle.net/d7XmQ/8/

"use strict";

var animalsXMLurl = 'http://dl.dropboxusercontent.com/u/27854284/Stuff/Online/XML_animals.xml';

$(function () {

    var $menu = $('#menu');

    $.ajax({
        url: animalsXMLurl, // name of file you want to parse
        dataType: "xml",
        success: handleResponse,
        error: function () {
            console.log('Error: Animals info xml could not be loaded.');
        }
    });

    function handleResponse(xmlResponse) {
        var $data = parseResponse(xmlResponse);
        createMenu($data);
    }

    function parseResponse(xmlResponse) {
        return $("item", xmlResponse).map(function () {
            var $this = $(this);
            return {
                id: $this.find("animal_id").text(),
                title: $this.find("animal_title").text(),
                url: $this.find("animal_url").text(),
                category: $this.find("animal_category").text()
            };
        });
    }

    function createMenu($data) {
        var categories = {};
        $data.each(function (i, dataItem) {
            if (typeof categories[dataItem.category] === 'undefined') {
                categories[dataItem.category] = [];
            }
            categories[dataItem.category].push(dataItem);
        });
        $.each(categories, function (category, categoryItems) {
            var categoryItems = categories[category];
            $menu.append($('<h2>').text(category));
            $menu.append(createList(categoryItems));
        });
    }

    function createList(categoryItems) {
        var $list = $('<ul>');
        $.each(categoryItems, function (i, dataItem) {
            $list.append(createItem(dataItem));
        });
        return $list;
    }

    function createItem(dataItem) {
        return $('<li>').text(dataItem.title);
    }
});

这篇关于如何计算重复的数组元素? (JavaScript的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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