Reputation: 3393
I have JSON array like:
var data = [
{
"name": "Jim",
"age" : 25
},
{
"name": "Jerry",
"age": 27
}
];
in HTML:
<ul>
<li ng-repeat="val in data | filter:{age: age}:true">{{val.name}}</li>
</ul>
The above code is working fine as i want the output, but i don't want to use ng-repeat
where it creates loop and then i filter.
is there any another way where i can directly select the DATA from array where age = age
??
Upvotes: 4
Views: 16482
Reputation: 329
If you want to parse single value of name. like {"name":"value"}. so after $http post, i am assuming that your response in var data= response.
Now find, like console.log(data[0].name); Here, result will be Jim
Upvotes: 0
Reputation: 75
I realize this is an old thread but for completeness.
This works:
{{data.Systems[data.Detail.SystemId].System}}
This way, you don't need to filter the data
.
Upvotes: 1
Reputation: 48211
Filters can be used in any angular expression (there is nothing ngRepeat
-specific about them).
(They can be even used in JavaScript.)
E.g. the following expression displays the data associated with the first user (limitTo:1
) that has an age of 25 (filter:{age: 25}
), formatted as JSON (json
):
{{data | filter:{age: 25}:true | limitTo:1 | json}}
See, also, this short demo.
Upvotes: 6
Reputation: 5124
As mentioned by @link in comments, there is no way to pluck the object with the required age from the array without looping through it. However, you're right in thinking that using ng-repeat
here is not appropriate if you only want to display a single object from the array, so ideally the required object should be stored in your controller.
One approach would be to use $filter
in your controller instead:
$scope.obj_with_correct_age = $filter("filter")(data, {age: age});
<li>{{ obj_with_correct_age.name }}</li>
Upvotes: 4