简体   繁体   English

使用 AngularJS 对 JSON 对象数组进行排序

[英]Sorting Array of JSON Objects with AngularJS

I'm trying to sort an "entry" resource by months between different tables.我正在尝试在不同表之间按月对“条目”资源进行排序。

My data is as follows :我的数据如下:

[
    {
        id: 1,
        date: "2011-04-01",
        text: "My first entry"
    },
    {
        id: 2,
        date: "2011-06-07",
        text: "My second entry"
    },
    ...
]

And I want to display the entries in different tables based on the month.我想根据月份显示不同表格中的条目。

I've made a "months" array and tried this :我制作了一个“月”数组并尝试了这个:

<div ng-repeat="month in months">
    <h3>{{month}}</h3>
    <ul ng-repeat="entry in entries">
        {{entry.text}}
    </ul>
</div>

Of course, this puts every entry in every month.当然,这会将每个条目放入每个月。 How can I only display the entries that belong in each month ?如何只显示属于每个月的条目?

You could use the "filter" filter:您可以使用“过滤器”过滤器:

<div ng-repeat="month in months">
    <h3>{{month}}</h3>
    <ul ng-repeat="entry in entries | filter:{date:month}">
        {{entry.text}}
    </ul>
</div>

You'll need to do some manipulation of the date property to compare to the month.您需要对日期属性进行一些操作以与月份进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM