1

I have the following angular array for one of my select tags

var names= [{name: 'Superman', val: '1'}, {name: 'Batman', val: '2'}];
$scope.names =names;
$scope.FormData = {};
$scope.FormData.name = $scope.names[1];

In the above array, instead of selecting the item by index (1), how can I select the item by name or the val, like

$scope.FormData.name = $scope.names['2']; #will select the {name: 'Batman', val: '2'}

I initially posted a lengthy question here, but then I realized this is the more simpler / focus way of my question

6
  • You can use the great Lodash library: lodash.com/docs#find Commented Sep 1, 2014 at 22:37
  • If you're talking about a select element you should show that too and clarify your question. Commented Sep 1, 2014 at 22:40
  • not clear what issue is. Can access the whole object using ng-options correctly. Need more details about problem Commented Sep 1, 2014 at 22:42
  • What is your issue here? Displaying in the ng-option or selecting the ng-Model data from the array for default selection? Commented Sep 1, 2014 at 22:44
  • 1
    probably this is an x/y problem which makes it look like a pure javascript question. Commented Sep 1, 2014 at 22:52

4 Answers 4

7

If you are using angular, why not use the build in $filter?

https://docs.angularjs.org/api/ng/filter/filter

In your case, if you want to filter in the controller, the code bellow will retrieve an Array with all the occurrences inside names that match the search for 'Superman':

$filter('filter')($scope.names, {name:'Superman'})

If you are confident that there will be at least one match you could do this in order to get the first match:

var matches = $filter('filter')($scope.names, {name:'Superman'});
var match = matches[0]:

Bare in mind that if there are no matches then match will be undefined

Sign up to request clarification or add additional context in comments.

Comments

2

If you want a re-useable function you could do it like this:

$scope.find = function(list, attributeName, attributeValue){
    var returnVal = {};
    angular.forEach(list,function(item){
        if(item[attributeName]){
          if(item[attributeName] === attributeValue){
            returnVal = item;
          }
        }
    });
    return returnVal;
};

$scope.FormData.name = $scope.find(names,'name','Batman');

Comments

2

In the above array, instead of selecting the item by index (1), how can I select the item by name

You can use the find method of the Lodash library (http://lodash.com/docs#find):

var names= [{name: 'Superman', val: '1'}, {name: 'Batman', val: '2'}];

var matchingBatman = _.find(names, { name: 'Batman' });

matchingBatman is: {name: 'Batman', val: '2'}

You can do the same if you want to find by val:

var matchingBatman = _.find(names, { val: '2' });

4 Comments

thanks for answer, I liked the idea of $filter as it seems more angular, but this will be a library I will surly look in to
@sameera207 Yes, the solution with $filter is equivalent, so make use of it. Lodash is useful for many complex manipulations of arrays/collections, I'm using it a lot :)
I'm pretty new to angular and I think its just mater of days/hours I need to start using Lodash :)
Everytime you'll need to manipulate arrays/collections, look at what Angular has, and if it hasn't a pretty way to do what you exactly want, Lodash is likely to be the key.
0

If you want to write a function instead of using a library

function selectByProperty(key, val){

    for(var i=0; i< $scope.names.length; i++){
        if($scope.names[i][key] === val){
           return $scope.names[i];
         }
     }

}
$scope.FormData.name  = selectByProperty('val','2');

2 Comments

Why would you want to iterate once you have found a match
thanks for answer, I liked the idea of $filter as it seems more angular

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.