jquery - 使用 jQuery 嵌套无序列表的对象

标签 jquery json object underscore.js

我一直在尝试将此对象转换为无序列表,但我需要将其合并到类别中。

var snippets = [
  {title: 'snippet 1', content: 'hello there 1', category: 'javascript'},
  {title: 'snippet 2 ', content: 'hello there 2', category: 'wordpress'},
  {title: 'snippet 3', content: 'hello there 3', category: 'javascript'},
  {title: 'snippet 4', content: 'hello there 4', category: 'general'},
  {title: 'snippet 5', content: 'hello there 5', category: 'general'}
];

预期输出是:

<ul>
  <li>javascript 
    <ul>
      <li>snippet 1</li>
      <li>snippet 2</li>
    </ul>
  </li>
    <li>wordpress 
    <ul>
      <li>snippet 1</li>
      <li>snippet 2</li>
    </ul>
  </li>
</ul>

我一直在尝试使用 $.each 来做到这一点,但是对于我的文件,我无法理解它。我也愿意使用 UnderscoreJS。

$('body').append('<ul class="categories"></ul>');

$.each(snippets, function(i, v) {
    var data = '<li class="category">'+ v.category +' ';
      data += '<ul class="item"> <li> ' + v.title + ' </li> </ul>';
      data += '</li>';

    $(data).appendTo('.categories');
});

输出不是预期的:

<ul class="categories">
  <li class="category">javascript 
    <ul class="item"> 
      <li> snippet 1 </li> 
    </ul></li>
  <li class="category">wordpress 
    <ul class="item"> 
      <li> snippet 2  </li> 
    </ul>
  </li>
  <li class="category">javascript 
    <ul class="item"> 
      <li> snippet 3 </li> 
    </ul>
  </li>
  <li class="category">general 
    <ul class="item"> 
      <li> snippet 4 
      </li> 
    </ul>
  </li>
  <li class="category">general 
    <ul class="item"> 
      <li> snippet 5 
      </li> 
    </ul>
  </li>
</ul>

谁能给我提示吗?

最佳答案

内嵌注释作为说明:

var snippets = [
  {title: 'snippet 1', content: 'hello there 1', category: 'javascript'},
  {title: 'snippet 2 ', content: 'hello there 2', category: 'wordpress'},
  {title: 'snippet 3', content: 'hello there 3', category: 'javascript'},
  {title: 'snippet 4', content: 'hello there 4', category: 'general'},
  {title: 'snippet 5', content: 'hello there 5', category: 'general'}
];

// Create the UL that will contain all the categories
var ul = $("<ul>").addClass("categories");
// Use an object as a map of category to sublist
var lists = Object.create(null);
// Loop through the snippets
snippets.forEach(function(snippet) {
  // Get the sublist for this category
  var list = lists[snippet.category];
  if (!list) {
    // Don't have one yet, create it
    list = lists[snippet.category] = $("<ul>");
    // Create the item that will contain it in the main list
    var item = $("<li>").addClass("category").text(snippet.category);
    // Add the sublist to the item, and the item to the main list
    item.append(list);
    ul.append(item);
  }
  // Add this entry to the sublist
  list.append($("<li>").addClass("item").text(snippet.title));
});
// Done building, add the main list to the doc
ul.appendTo(document.body);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于jquery - 使用 jQuery 嵌套无序列表的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737615/

相关文章:

javascript - d3 在追加圆圈时跳过数组中的第一个索引

java - 从子类的arraylist到达父类方法

jquery - 使用 jQuery 更改占位符文本

jquery - 检测特定图像是否已加载然后执行某些操作...

jquery - 动画功能比其他功能完成

javascript - 在 PHP、MySQL 和 JS 站点上存储琐碎数据的有效方法

json - 在 Erlang 中处理 badarg

jquery - 全页网站并使用鼠标滚轮滚动浏览各个部分

json - 使用 Kotlin 数据类在 Jackson 中区分 null 和缺失的属性

c++ - 我继承了一个使用私有(private)的构造函数,为什么我仍然可以从主函数访问它?