arrays - Add condition to AngularJS map function -


i'm using below loop through array , add property each item of array.

$scope.addresses = $scope.addresses.map(function(address) {       address.location = "x";          return address; }); 

this @ end return items. how can add condition loop? can check property , based on property return addresses property true?

any appreciated!

you should use filter function

you can read here: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/filter

basically filter function gets boolean function parameter (let's call f), , returns array items f returns true for.

for example:

var mapped = $scope.addresses = $scope.addresses.map(function(address) {       address.location = "x";          return address; });  var filtered = mapped.filter(function (address) {     return address.location.length > 5; }); 

filtered hold collection of addresses have location more 5 characters.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -