rram rram - 1 year ago 115
AngularJS Question

Find object by its property in array of objects with angular way

I have a array like below

var myArray = [{'id':'73','name':'john'},{'id':'45','name':'Jass'}, etc.]

Now I have an
73 how to select this particular object from the array. I see I can do this in jQuery easily with grep Is there any angular way of doing this?

Since most user developing app with angular always get data from array of objects(mostly for table) there should be a helper function for this?

So that I can change the data of the row with the ID of the row by updating the array of object.

I don't want to bind this in view. I want to manipulate the data and update the data withing function.

For Example. I have a table listing. If end user edit a row from the table I have the ID of the object so after end user hit save, I need to update the array and then back to table listing.

Answer Source

you can use angular's filter https://docs.angularjs.org/api/ng/filter/filter

in your controller:

$filter('filter')(myArray, {'id':73}) 

or in your HTML

{{ myArray | filter : {'id':73} }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download