0

I am developing web application in angularjs. I have one foreach loop to loop through scope object. Below is my js code.

$scope.details = []; $scope.apply = function() { angular.forEach($scope.screenMap, function(value, key) { //$scope.details.push(value.scrn_id, value.Read, value.Write); $scope.details.push("ID", value.scrn_id); $scope.details.push("Read", value.Read); $scope.details.push("Write", value.Write); }); console.log($scope.details); } 

After looping I get total 18 entries for 3 iterations.

What I am expecting is

 ID:1 Read:True Write: False ID:2 Read:False Write: True ID:3 Read:True Write: False ID:4 Read:False Write: True 

may I know how can I get array in above format?

1
  • Unable to create array of obejcts in angularjs in foreach loop, but you didn't create an object. You are making an array of properties. Combine the properties in an object and then push.
    – Sajal
    CommentedJul 5, 2017 at 11:17

5 Answers 5

2

You have to push to the array the values as an object to achieve this.

$scope.details = []; $scope.apply = function () { angular.forEach($scope.screenMap, function (value, key) { //$scope.details.push(value.scrn_id, value.Read, value.Write); $scope.details.push({"ID":value.scrn_id,"Read":value.Read,"Write":value.Write}); }); console.log($scope.details); } 
    1

    push it as an object

    $scope.details = []; $scope.apply = function () { angular.forEach($scope.screenMap, function (value, key) { $scope.details.push({"ID": value.scrn_id, "Read": value.Read, "Write": value.Write}); }); console.log($scope.details); } 
    1
    • @NiranjanGodbole You should consider who answered first while accepting answers. :(
      – Vivz
      CommentedJul 5, 2017 at 11:30
    1

    Try this code

    $scope.details = []; $scope.apply = function () { angular.forEach($scope.screenMap, function (value, key) { var obj = { "ID" : value.scrn_id, "Read" : value.Read, "Write" : value.Write }; $scope.details.push(obj); }); console.log($scope.details); } 
      1
       angular.forEach($scope.screenMap, function (value, key) { $scope.details.push({ "ID": value.scrn_id, "Read": value.Read, "Write", value.Write }); }); 
        1

        Your pushing 3 times in a loop that too two objects at a time. Dont use commas to insert key value pair(object name : value)
        javascript Object annotation is as follows var x = {"nameofthe Object1":object value1, "nameofthe Object2":object value2 } 3*2*3 = 18 and these are single entries.

        create a javascript object and push into the $scope.details

         $scope.details = []; $scope.apply = function () { angular.forEach($scope.screenMap, function (value, key) { //$scope.details.push(value.scrn_id, value.Read, value.Write); $scope.details.push({"ID": value.scrn_id, "Read": value.Read, "Write": value.Write}); }); console.log($scope.details); } 

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.