0

I have a contoroller:

$scope.getFromDB=function(data)
{
  $scope.nameSelected=[];
  var myCookie=$cookie.get("nameCookie");
   $scope.names= data;
for(var i=0; i<$scope.names.length;i++)
{
   if($scope.names._id=myCookie._id)
     {
       $scope.nameSelected.push($scope.names[i]);
      }
  }

I want display the user's firstname, middlename, lastname stored in the $scope.nameSelected, since the nameSelectedis in the array form, name are got in array nameSelected[0].firstname, nameSelected[0].middlename,nameSelected[0].lastname. So how to convert it into a object so it is easy to access the names.

2 Answers 2

1

From what you've written,you don't need an array at all, just use an object. Change this:

$scope.nameSelected=[];
...
$scope.nameSelected.push($scope.names[i]);

to

$scope.nameSelected = null;
...
$scope.nameSelected = $scope.names[i];
Sign up to request clarification or add additional context in comments.

Comments

0

Based on what you've mentioned in your question, it is already an object at the index 0 of your nameselected array, That means, doing,

$scope.nameselected[0]

should give you this,

{firstName : "Robert", middlename :"Downy", lastName:"junior"}

Similarly, for all the names as in your loop ,

$scope.nameselected = $scope.names[i]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.