I have a data model like this:
var array = [{id:1, name:"foo"}, {id:2, name:"bar"}, {id:3 name:"september"}];
I want to loop through the array and see if id:3 exists how do i do that?
I have a data model like this:
I want to loop through the array and see if id:3 exists how do i do that? |
|||
Just use an AngularJS filter, these are exactly made for that purpose:
If the length of The advantage of using a filter is that you can easily filter on multiple attribute values by extending the filter expression Cheers, |
|||||
|
This is more a js problem than an angular one. If you are using a library like lodash this is pretty straightforward :
In plain javascript this is not much complicated. Here is an EcmaScript 5 solution, that makes use of the function some :
|
||||
|
Wonder, you are missing a It's more of a javaScript looping over an Array than an AngularJS way,
|
||||
|
You don't need lodash, even. Native javascript:
will return an array containing only the element whose id is 3 if there is one, or an empty array if not. |
|||
|
you can do it with plain JavaScript:
|
|||||
|
id
property equals3
– Jan Jul 6 '15 at 22:27,
afterid:3
? updated my answer. – Sudhansu Choudhary Jul 7 '15 at 12:37