Get specific object by id from array of objects in AngularJS

前端 未结 17 1387
囚心锁ツ
囚心锁ツ 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:17
        projectDetailsController.controller('ProjectDetailsCtrl', function ($scope, $routeParams, $http) {
        $http.get('data/projects.json').success(function(data) {
    
            $scope.projects = data;
            console.log(data);
    
            for(var i = 0; i < data.length; i++) {
            $scope.project = data[i];
            if($scope.project.name === $routeParams.projectName) {
                console.log('project-details',$scope.project);
            return $scope.project;
            }
            }
    
        });
    });
    

    Not sure if it's really good, but this was helpful for me.. I needed to use $scope to make it work properly.

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

    If you want the list of items like city on the basis of state id then use

    var state_Id = 5;
    var items = ($filter('filter')(citylist, {stateId: state_Id }));
    
    0 讨论(0)
  • 2020-12-07 08:18

    I would iterate over the results array using an angularjs filter like this:

    var foundResultObject = getObjectFromResultsList(results, 1);

    function getObjectFromResultsList(results, resultIdToRetrieve) {
            return $filter('filter')(results, { id: resultIdToRetrieve }, true)[0];
        }
    
    0 讨论(0)
  • 2020-12-07 08:19

    use $timeout and run a function to search in "results" array

    app.controller("Search", function ($scope, $timeout) {
            var foo = { "results": [
              {
                 "id": 12,
                 "name": "Test"
              },
              {
                 "id": 2,
                 "name": "Beispiel"
              },
              {
                 "id": 3,
                "name": "Sample"
              }
            ] };
            $timeout(function () {
                for (var i = 0; i < foo.results.length; i++) {
                    if (foo.results[i].id=== 2) {
                        $scope.name = foo.results[i].name;
                    }
                }
            }, 10);
    
        });
    
    0 讨论(0)
  • 2020-12-07 08:21

    The simple way to get (one) element from array by id:

    The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

    function isBigEnough(element) {
        return element >= 15;
    }
    
    var integers = [12, 5, 8, 130, 160, 44];
    integers.find(isBigEnough); // 130  only one element - first
    

    you don't need to use filter() and catch first element xx.filter()[0] like in comments above

    The same for objects in array

    var foo = {
    "results" : [{
        "id" : 1,
        "name" : "Test"
    }, {
        "id" : 2,
        "name" : "Beispiel"
    }, {
        "id" : 3,
        "name" : "Sample"
    }
    ]};
    
    var secondElement = foo.results.find(function(item){
        return item.id == 2;
    });
    
    var json = JSON.stringify(secondElement);
    console.log(json);
    

    Of course if you have multiple id then use filter() method to get all objects. Cheers

    function isBigEnough(element) {
        return element >= 15;
    }
    
    var integers = [12, 5, 8, 130, 160, 44];
    integers.find(isBigEnough); // 130  only one element - first

    var foo = {
    "results" : [{
        "id" : 1,
        "name" : "Test"
    }, {
        "id" : 2,
        "name" : "Beispiel"
    }, {
        "id" : 3,
        "name" : "Sample"
    }
    ]};
    
    var secondElement = foo.results.find(function(item){
        return item.id == 2;
    });
    
    var json = JSON.stringify(secondElement);
    console.log(json);

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