Search in an array for a specific string and return the value in the AngularJS view

advertisements

In the code below, I'm trying to search the JSON output for a specific string (for example 'name_col_lbl') and return its value (in this case 'Name') for AngularJS to ouptut in the view.

$scope.globalContent = [
    {"id":"1","module":"student_reg","item":"name_col_lbl","value":"Name"},
    {"id":"2","module":"student_reg","item":"bday_col_lbl","value":"Birthdate"}
]
angular.forEach($scope.globalContent, function(el){
    $scope.nameLbl = el ***This is where I need to search for the string and return its value***;
    $scope.bdayLbl= el ***This is where I need to search for the string and return its value***;

});

I can't seem to find an efficient way to handle this. Thanks in advance!


This should do the trick:

var $scopeglobalContent = [
    {"id":"1","module":"student_reg","item":"name_col_lbl","value":"Name"},
    {"id":"2","module":"student_reg","item":"bday_col_lbl","value":"Birthdate"}
];

for(var i = 0; i < $scopeglobalContent.length; i++){
    for(key in $scopeglobalContent[i]){
    if($scopeglobalContent[i][key] == "name_col_lbl"){
        return console.log($scopeglobalContent[i].value);
    }
  }
}

This is basic stuff. I suggest you read on objects and loops to get a better understanding of how this works and how to use it.