Filtar object angularjs

1

I have:

var members = [
        { username: 'SheoNarayan', address: 'Hyderabad', pin : '500049' },
        { username: 'Munna', address: 'Bokaro', pin: '8256598' },
        { username: 'Jay', address: 'Aurangabad', pin: '824101' },
        { username: 'Sreeni', address: 'New York', pin: 'BY-524' }
];

and in my html:

Jay : {{members | filter: { username: 'Jay'} }}  => esto me muestra { username: 'Jay', address: 'Aurangabad', pin: '824101' }

But I just want to show Jay's pin and I do not want to do a ng-repeat going through all to do a ng-if , is there any way I can get what I want directly?

    
asked by sirdaiz 09.08.2017 в 11:50
source

2 answers

2

I do not know if you can directly access a property of the object in the way you're looking for, but I can think of the following.

<li ng-repeat="member in members | filter: { username: 'Jay'}">
 <p>{{member.username}}:{{member.pin}}</p>
</li>

Or if you look very motivated you make your own filter:

app.filter('nameFilter', function() {
    return function(elements,name) {
        for(elem in elements){
          if(elements[elem].username==name){
            return elements[elem].pin
          }
        }

        return name;
    };
});

Jay : {{members | nameFilter : 'Jay'}}

Here the plunkr

    
answered by 09.08.2017 / 13:48
source
1

My recommendation for these things is the Lodash library.

  

Here is your official page

For these things, for example, you have the find method, from which you can see its documentation here

In code your problem would be solved:

JS

var members = [
        { username: 'SheoNarayan', address: 'Hyderabad', pin : '500049' },
        { username: 'Munna', address: 'Bokaro', pin: '8256598' },
        { username: 'Jay', address: 'Aurangabad', pin: '824101' },
        { username: 'Sreeni', address: 'New York', pin: 'BY-524' }
];

var userJay = _.find(memebers, ['username', 'Jay']);

HTML

Jay : {{ userJay.pin }}
    
answered by 09.08.2017 в 17:13