0

I have a array in my controller Which has assign multiple keys with the value i need to access these values in controller so that i can save these data to database

Array is given below:-

 $scope.Notes['surfacedefault-1'] = { value: "xyz" };
 $scope.Notes['surfacedefault-2'] = { value: "we" };
 $scope.Notes['surfacedefault-3'] = { value: "123" };
 $scope.Notes['surfacedefault-4'] = { value: "red" };
 $scope.Notes['surfacedefault-5'] = { value: "blue" };

Please suggest me to access their value in controller using foreach loop

4 Answers 4

1

It should be like this

angular.forEach($scope.Notes, function(value, key) {
  console.log(key + ': ' + value);
});

Also you can make use of .push to create key value pairs in array, eg:

 $scope.Notes.push({'surfacedefault-1':'xyz'});
Sign up to request clarification or add additional context in comments.

Comments

1

use forEach in angular

   angular.forEach($scope.Notes, function (val, key) {
        console.log(val.value)
    })

DEMO

2 Comments

Thank You It was useful for me
@AshutoshAdarsh Welcome :)
0

that could be a solution?

$scope.Notes=[
    {name:'surfacedefault-1', value: "xyz" },
    {name:'surfacedefault-2', value: "we" },
    {name:'surfacedefault-3', value:"123" },
    {name:'surfacedefault-4',value: "red" },
    {name:'surfacedefault-5',value: "blue" }];
for(var i=0;i<$scope.notes.length;i++{
    do something
}

Comments

0

Use this.

var array = Object.keys($scope.Notes).map(function(key, index, array){  
   return $scope.Notes[key].value
})

Comments

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.