javascript - 按类别打破 JSON 字符串介绍有序列表

标签 javascript jquery json each

我正在尝试将 JSON 对象放入按大陆和国家排列的无序列表中。

JSON 看起来像:

var mylocations = '{
    "Locations":[
        {"Place":"Africa_Egypt"},
        {"Place":"Africa_SouthAfrica"},
        {"Place":"Africa_SouthAfrica"},
        {"Place":"Africa_SouthAfrica"},
        {"Place":"Americas_Brazil"},
        {"Place":"Americas_Canada"},
        {"Place":"Americas_USA"},
        {"Place":"Europe_Switzerland"},
        {"Place":"Europe_UK"}
    ]
}';

这是我到目前为止所拥有的:

arr = $.parseJSON(mylocations);
var continent = {},
    country;

$.each(arr.Locations, function( i, el ) {
    var ul = $('<ul></ul>');
    country = el.Place.substr(0, el.Place.indexOf("_"));

    if (continent.hasOwnProperty(country)) {
        continent[country] += 1;
        children = $('<li>' + country + '</li>');
        children.appendTo('ul')
    }
    else {
        $('#country-list').append(ul);
        continent[country] = 1;
    }
});

// print results
for(var key in continent){
    console.log(key + ' (' + continent[key] + ')');
}

我追求的是:

<ul>
    <li>
        Continent
        <ul>
            <li>Country</li>
            <li>Country</li>
        </ul>
    </li>
    <li>
        Continent
        <ul>
            <li>Country</li>
            <li>Country</li>
        </ul>
    </li>
</ul>

努力让这个工作正常进行。这是我到目前为止所拥有的: jsfiddle

如有任何帮助,我们将不胜感激。

最佳答案

这是我的解决方案。这不是最好的,但我希望这不是最坏的:D 对于任何问题/投诉,我都在这里。希望对您有帮助

console.log(arr)
var ul = $('<ul></ul>');
var mylocations = '{"Locations":[{"Place":"Africa_Egypt"},{"Place":"Africa_SouthAfrica"},{"Place":"Africa_SouthAfrica"},{"Place":"Africa_SouthAfrica"},{"Place":"Americas_Brazil"},{"Place":"Americas_Canada"},{"Place":"Americas_USA"},{"Place":"Europe_Switzerland"},{"Place":"Europe_UK"}]}';
  
var arr = $.parseJSON(mylocations);

var locations = {}
$.each(arr.Locations, function( i, el ) {
  var delimiter = el.Place.indexOf("_");
  var continent = el.Place.substr(0, delimiter);
  var country = el.Place.substr(delimiter+1, el.Place.length);

  locations[continent] = locations[continent] || []
  locations[continent].push(country)
});

for(var continent in locations) {
  $('<li>' + continent + '</li>').appendTo(ul)
  var continentUL = $('<ul></ul>');
  for(var countryIndex in locations[continent]) {
      $('<li>' + locations[continent][countryIndex] + '</li>').appendTo(continentUL)
  }	
  continentUL.appendTo(ul)
}

ul.appendTo($('#country-list'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="country-list">
</div>

关于javascript - 按类别打破 JSON 字符串介绍有序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930790/

相关文章:

javascript - 如何从正则表达式的结果中获取数组

javascript - 交替按钮不起作用

javascript - 调用函数时为"Uncaught TypeError: object is not a function"

jquery - jQuery 中的混合字符串和变量

c# - 无法从使用 C# 开发的 Android 应用程序接收 json

javascript - 如何从一个div中选择一个元素到另一个?

javascript - 数据表行未定义(DataTables.js)

javascript - 如何使用 HTML 中的 JSON 外部文件填充下拉按钮

python - 如何在深度嵌套的 json 文件中搜索某个键的所有出现位置?

jquery - ajax请求的json不填充流程图