Jordash
Jordash

Reputation: 3093

Grab specific value from Array of Objects based on value of object

I have an array of objects like this:

$scope.SACCodes = [
    {'code':'023', 'description':'Spread FTGs', 'group':'footings'},
    {'code':'024', 'description':'Mat FTGs', 'group':'footings'}
]

I want to write a function to grab the description based on the code, something like this:

$scope.SACDescription = function(code) {
     return $scope.SACCodes WHERE code=:code
}

I'm not sure of the proper syntax?

Upvotes: 0

Views: 38

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You can use Array.prototype.filter()

Like this

$scope.SACDescription = function(code) {
     return $scope.SACCodes.filter(function(x){ return x.code == code; });
}

DEMO

Upvotes: 1

Related Questions