admin管理员组

文章数量:1318028

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

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

Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Sep 1, 2014 at 22:34 sameera207sameera207 16.6k19 gold badges91 silver badges155 bronze badges 6
  • You can use the great Lodash library: lodash./docs#find – Mik378 Commented Sep 1, 2014 at 22:37
  • If you're talking about a select element you should show that too and clarify your question. – Jason Goemaat 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 – charlietfl 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? – PSL Commented Sep 1, 2014 at 22:44
  • 1 probably this is an x/y problem which makes it look like a pure javascript question. – PSL Commented Sep 1, 2014 at 22:52
 |  Show 1 more ment

4 Answers 4

Reset to default 7

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

https://docs.angularjs/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

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');

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./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' });

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');

本文标签: javascriptAngularJS select array hash element by namevalueStack Overflow