10

私のページにはangularjsを使用しています。冗長性がないように、JSON オブジェクトから値をフィルター処理したい。しかし、Angular ng-repeat から一意の値を取得する方法が見つかりませんでした。とにかくそれを行うことはありますか?

わかりました、ここに質問についての説明があります。この形式の JSON があります。この JSON は、サービスから取得しています。したがって、繰り返しデータがどのように発生するかは予測できません。

result = [
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        },      
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        },      
        {
            _id: "500005",
            subject: "Attend to the event",
            date: "2 Jan, 2013"
        },      
        {
            _id: "500065",
            subject: "Some task deadline",
            date: "20 Sep, 2013"
        },      
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        }
]

出力JSONに要素が繰り返されないようにしたいので、出力は次のようになります

result = [
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        },      
        {
            _id: "500005",
            subject: "Attend to the event",
            date: "2 Jan, 2013"
        },      
        {
            _id: "500065",
            subject: "Some task deadline",
            date: "20 Sep, 2013"
        }
]
4

3 に答える 3

17

uniqueフィルターが定義された Angular UI を利用できます。

ソースはここにあります。

基本的に、次のようにフィルターを使用できます。

<div ng-repeat="item in result | unique:'_id'">
    //Body here
</div>
于 2013-09-11T17:58:50.423 に答える
5

angular.filter モジュール ( https://github.com/a8m/angular-filter ) で「unique」(エイリアス: uniq) フィルターを使用できます。

使用法:colection | uniq: 'property'
ネストされたプロパティで次のようにフィルタリングできます。colection | uniq: 'property.nested_property'

だからあなたはそのようなことをすることができます..

function MainController ($scope) {
 $scope.orders = [
  { id:1, customer: { name: 'foo', id: 10 } },
  { id:2, customer: { name: 'bar', id: 20 } },
  { id:3, customer: { name: 'foo', id: 10 } },
  { id:4, customer: { name: 'bar', id: 20 } },
  { id:5, customer: { name: 'baz', id: 30 } },
 ];
}

HTML:顧客 ID でフィルタリングします。つまり、重複した顧客を削除します。

<th>All customers list: </th>
<tr ng-repeat="order in orders | unique: 'customer.id'" >
   <td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>

結果: すべての顧客リスト:
foo 10
bar 20
baz 30

于 2014-07-21T05:17:17.150 に答える
3
var data = [
    {
        _id: "500004",
        subject: "Complete the task",
        date: "25 Aug, 2013"
    },      
    {
        _id: "500004",
        subject: "Complete the task",
        date: "25 Aug, 2013"
    },      
    {
        _id: "500005",
        subject: "Attend to the event",
        date: "2 Jan, 2013"
    },      
    {
        _id: "500065",
        subject: "Some task deadline",
        date: "20 Sep, 2013"
    },      
    {
        _id: "500004",
        subject: "Complete the task",
        date: "25 Aug, 2013"
    }
]

var uniqueNames = [];
var uniqueObj = [];

for(i = 0; i< data.length; i++){    

    if(uniqueNames.indexOf(data[i]._id) === -1){
        uniqueObj.push(data[i])
        uniqueNames.push(data[i]._id);        
    }       

}

console.log('uniqueObj',uniqueObj)

http://jsfiddle.net/C97DJ/165/

于 2016-04-22T17:34:55.310 に答える