angularjs - Push objects (same keys names) in array using angular.forEach () -


how push objects same key name array using angular.foreach() function ?

for example, declare empty array i.e. $scope.arr = [] , empty object i.e. $scope.obj = {}

now using angular.foreach() , push() function, how can following result :

$scope.arr = [{msg: ''}, {msg: 'please, enter no.'}, {msg: ''}]

my js code

$scope.arr = [] $scope.obj = {}        angular.foreach(['12', 'please, enter no.', '43'], function (value, index) {            if (isnan (value)) {                 $scope.obj['msg'] = 'please, enter no.';                 $scope.arr.push($scope.obj);            }            else {                 $scope.obj['msg'] = '';                 $scope.arr.push($scope.obj);            }        }); 

current wrong output : $scope.arr = [{msg: 'please, enter no.'}, {msg: 'please, enter no.'}, {msg: 'please, enter no.'}]

expected output : $scope.arr = [{msg: ''}, {msg: 'please, enter no.'}, {msg: ''}]

i knew why got wrong output because msg keys updated last value of array, not find remedy this.

please, me.....

why create $scope object? have found keep adding same object reference array. how about:

angular.foreach(['12', '43', 'please, enter no.'], function (value, index) {     if (isnan (value)) {         $scope.arr.push({ msg: 'please, enter no.' });     }     else {         $scope.arr.push({ msg: '' });     } }); 

Comments