Get specific object by id from array of objects in AngularJS

前端 未结 17 1388
囚心锁ツ
囚心锁ツ 2020-12-07 07:20

I have a JSON file containing some data I d like to access on my AngularJS website. Now what I want is to get only one object from the array. So I d like for example Item wi

相关标签:
17条回答
  • 2020-12-07 08:08

    The only way to do this is to iterate over the array. Obviously if you are sure that the results are ordered by id you can do a binary search

    0 讨论(0)
  • 2020-12-07 08:12

    Using ES6 solution

    For those still reading this answer, if you are using ES6 the find method was added in arrays. So assuming the same collection, the solution'd be:

    const foo = { "results": [
        {
            "id": 12,
            "name": "Test"
        },
        {
            "id": 2,
            "name": "Beispiel"
        },
        {
            "id": 3,
            "name": "Sample"
        }
    ] };
    foo.results.find(item => item.id === 2)
    

    I'd totally go for this solution now, as is less tied to angular or any other framework. Pure Javascript.

    Angular solution (old solution)

    I aimed to solve this problem by doing the following:

    $filter('filter')(foo.results, {id: 1})[0];
    

    A use case example:

    app.controller('FooCtrl', ['$filter', function($filter) {
        var foo = { "results": [
            {
                "id": 12,
                "name": "Test"
            },
            {
                "id": 2,
                "name": "Beispiel"
            },
            {
                "id": 3,
                "name": "Sample"
            }
        ] };
    
        // We filter the array by id, the result is an array
        // so we select the element 0
    
        single_object = $filter('filter')(foo.results, function (d) {return d.id === 2;})[0];
    
        // If you want to see the result, just check the log
        console.log(single_object);
    }]);
    

    Plunker: http://plnkr.co/edit/5E7FYqNNqDuqFBlyDqRh?p=preview

    0 讨论(0)
  • 2020-12-07 08:13

    You can use ng-repeat and pick data only if data matches what you are looking for using ng-show for example:

     <div ng-repeat="data in res.results" ng-show="data.id==1">
         {{data.name}}
     </div>    
    
    0 讨论(0)
  • 2020-12-07 08:14

    I just want to add something to Willemoes answer. The same code written directly inside the HTML will look like this:

    {{(FooController.results | filter : {id: 1})[0].name }}
    

    Assuming that "results" is a variable of your FooController and you want to display the "name" property of the filtered item.

    0 讨论(0)
  • 2020-12-07 08:14

    Why complicate the situation? this is simple write some function like this:

    function findBySpecField(data, reqField, value, resField) {
        var container = data;
        for (var i = 0; i < container.length; i++) {
            if (container[i][reqField] == value) {
                return(container[i][resField]);
            }
        }
        return '';
    }
    

    Use Case:

    var data=[{
                "id": 502100,
                "name": "Bərdə filialı"
            },
            {
                "id": 502122
                "name": "10 saylı filialı"
            },
            {
                "id": 503176
                "name": "5 sayli filialı"
            }]
    
    console.log('Result is  '+findBySpecField(data,'id','502100','name'));
    

    output:

    Result is Bərdə filialı
    
    0 讨论(0)
  • 2020-12-07 08:16

    personally i use underscore for this kind of stuff... so

    a = _.find(results,function(rw){ return rw.id == 2 });
    

    then "a" would be the row that you wanted of your array where the id was equal to 2

    0 讨论(0)
提交回复
热议问题