English 中文(简体)
Java印物中的标语
原标题:Alphabetize items in a JavaScript object?

我有这样的目标:

countrylist:{
        regions:[
            {
                region: "Europe",
                countries: [
                    {
                        "country":"Albania",
                        "href":"eu",
                        "locale":"en_al"
                    },
                    {
                        "country":"France",
                        "href":"eu",
                        "locale":"en_fr"
                    },
                    {
                        "country":"Ireland",
                        "href":"eu",
                        "locale":"en_ie"
                    }]      
                },
                    region: "Asia",
                    countries: [
                        {
                            "country":"China",
                            "href":"as",
                            "locale":"ch"
                        },
                        {
                            "country":"Japan",
                            "href":"as",
                            "locale":"jp"
                        },
                        {
                            "country":"Thailand",
                            "href":"as",
                            "locale":"th"
                        }]      
                    }
                 ]}

如果你能看到整个目标,你就会看到,它按区域分类,每个区域内的国家都以字母顺序排列。 然而,我需要把所有国家的下降菜单按字母顺序排列,而不是按区域分类。 对这些物品进行分类的最清洁方法是什么?

我最初将该国推向一个空阵,并对此进行分类。 然而,我需要保持国家领域与其相应领域和当地领域之间的关系。

最佳回答

Initialize an empty array, then go through the regions and append all the countries to that array. When you re done, sort the array.

var countries = [];
for(var i = 0; i < countrylist.regions.length; i++)
    Array.prototype.push.apply(countries, countrylist.regions[i].countries);

countries.sort(function(a, b) {
    return a.country > b.country;
});

console.log(countries);

http://jsfiddle.net/Jehsb/

问题回答

你们需要走下结构,创造一系列的国家名称。 然后,你可以打上阵列,并再次这样做。

var arr = [];
for(var i = 0; i < countryList.regions.length; i++){
    var countries = countryList.regions[i].countries;
    for(var j = 0; j < countries.length; j++){
        arr.push(countries[j].country);
    }
}

arr.sort();

如果你也需要其他信息,那么你需要的是所有国家目标的统一体,然后适用一种习惯功能:

var arr = [];
for(var i = 0; i < countryList.regions.length; i++){
    var countries = countryList.regions[i].countries;
    for(var j = 0; j < countries.length; j++){
        arr.push(countries[j]);
    }
}

arr.sort(function(xx,yy){ 
    return xx.country < yy.country ? -1 : 1;
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签