【问题标题】:How to group one JSON property based on one other property that appears at many places?如何根据出现在许多地方的另一个属性对一个 JSON 属性进行分组?
【发布时间】:2017-05-18 03:16:45
【问题描述】:

我有这个 JSON

var books = [

    { author: "George Owell", title: "1984", price: 0, page: 328}, 
    { author: "Jane Austen", title: "Pride And Prejudice", price: 20, page: 279}, 
    { author: "J.K. Rowling", title: "Harry Potter and the Sorcerer's Stone", price: 23, page: 320}, 
    { author: "Plato", title: "Apology", price: 0, page: 127}, 
    { author: "Sigmund Freud", title: "The Interpretation of Dreams"},
    { author: "George Owell", title: "Animal Farm", price: 0, page: 100}, 
    { author: "George Owell", title: "Omh", price: 0, page: 100}, 
    { author: "Plato", title: "Aristotle", price: 0, page: 227}

];

现在我想制作一个输出作者和标题的表格。如果作者相同,则标题将合并在一起。例如,“George Owell”的标题为“1984, Animal Farm, Ohm.”,“Plato”的标题为“Apology, Aristotle”,等等。我如何使用 jQuery 或 vanilla JavaScript 来做到这一点?

【问题讨论】:

  • 您的问题中没有 JSON:您有一个对象数组。无论如何,我可能会为此使用数组.reduce() 方法。您是在询问如何生成 HTML 输出以显示结果,还是只是在询问如何合并标题?合并部分可以(或多或少)以与此问题相同的方式完成:stackoverflow.com/questions/33850412/…
  • 如果您不关心“价格”或“页面”,我可以非常快速地编写代码来执行此操作 - 这些是“书籍”,因此想要创建一个名为“作者”,那么我想这是有道理的......但我认为你可能试图以错误的方式解决另一个问题。

标签: javascript jquery


【解决方案1】:

var books = [
    { author: "George Owell", title: "1984", price: 0, page: 328}, 
    { author: "Jane Austen", title: "Pride And Prejudice", price: 20, page: 279}, 
    { author: "J.K. Rowling", title: "Harry Potter and the Sorcerer's Stone", price: 23, page: 320}, 
    { author: "Plato", title: "Apology", price: 0, page: 127}, 
    { author: "Sigmund Freud", title: "The Interpretation of Dreams"},
    { author: "George Owell", title: "Animal Farm", price: 0, page: 100}, 
    { author: "George Owell", title: "Omh", price: 0, page: 100}, 
    { author: "Plato", title: "Aristotle", price: 0, page: 227}
];

var uniqueauthors = [];
$.each(books,function(index,value){
if($.inArray(value.author,uniqueauthors) < 0)
{
uniqueauthors.push(value.author);
}
});

$.each(uniqueauthors,function(index,value){
var bok = $.map(books,function(val,index){
return (val.author == value) ? val.title : null;
});
var $tr = $('<tr>').append('<td>'+value+'</td>').append('<td>'+bok.join(",")+'</td>');
$('table').append($tr);
});
td{
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>Authors</td>
<td>Titles</td>
</tr>
</table>

【讨论】:

    【解决方案2】:

    以下内容应该可以帮助您入门:

    var books = [
    
        { author: "George Owell", title: "1984", price: 0, page: 328}, 
        { author: "Jane Austen", title: "Pride And Prejudice", price: 20, page: 279}, 
        { author: "J.K. Rowling", title: "Harry Potter and the Sorcerer's Stone", price: 23, page: 320}, 
        { author: "Plato", title: "Apology", price: 0, page: 127}, 
        { author: "Sigmund Freud", title: "The Interpretation of Dreams"},
        { author: "George Owell", title: "Animal Farm", price: 0, page: 100}, 
        { author: "George Owell", title: "Omh", price: 0, page: 100}, 
        { author: "Plato", title: "Aristotle", price: 0, page: 227}
        ];
    
    
       var authorBooks = {}
    
       books.forEach(function(book) {
    
          if(!authorBooks[book.author]) {
            authorBooks[book.author] = [];
          }
    
          authorBooks[book.author].push({title: book.title, price: book.price, page: book.page});
    
        },{});
    

    【讨论】:

      【解决方案3】:

      昨天在 python 中出现了同样的问题。这是我的回答:https://stackoverflow.com/a/44017627/7633845

      这是一个js版本:

      var map = {}
      for(var i = 0; i < books.length; i++) {
          var book = books[i];
          if(!(book.author in map)) {
              map[book.author] = []
          }
          map[book.author].push(book.title)
      }
      
      var out = []
      for(var author in map) {
          out.push({
              author: author,
              books: map[author].join(", ")
          })
      }
      

      【讨论】:

        【解决方案4】:

        你可以试试

        var books = [
        
        { author: "George Owell", title: "1984", price: 0, page: 328}, 
        { author: "Jane Austen", title: "Pride And Prejudice", price: 20, page: 279}, 
        { author: "J.K. Rowling", title: "Harry Potter and the Sorcerer's Stone", price: 23, page: 320}, 
        { author: "Plato", title: "Apology", price: 0, page: 127}, 
        { author: "Sigmund Freud", title: "The Interpretation of Dreams"},
        { author: "George Owell", title: "Animal Farm", price: 0, page: 100}, 
        { author: "George Owell", title: "Omh", price: 0, page: 100}, 
        { author: "Plato", title: "Aristotle", price: 0, page: 227}
        ];
        
        var results = books.reduce((previous, book) => {
        (previous[book.author] = previous[book.author] || []).push(book);
            return previous;
        }, []);
        
        console.log(results);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多