0

i have this object with arrays

$scope.filters = {     
      Sites: [], 
      Regions: [], 
      Devices : [], 
      Variables : [],      
     };

i want to push into Variables {'VariableName': 'Total Active Energy'};

i did this

     var obj = {};
      obj['VariableName'] = response.data[0].AutoReportVariable;
      $scope.filters.Variables.push(obj);        
      console.log($scope.filters.Variables);

but in console I take :

0:{VariableName:'Total Active Energy'}
how can i push the value to take in console only {VariableName:'Total Active Energy'} without key 0 ?

i want this to have

$scope.filters = {     
  Sites: [], 
  Regions: [], 
  Devices : [], 
  Variables : [{VariableName:'Total Active Energy'}]      
 };
4
  • that 0 is not key, its the index of array Commented May 8, 2018 at 7:55
  • You have the format you want! console.log shows you indexes too. What is your problem?? Maybe an object?? Commented May 8, 2018 at 7:56
  • console.log($scope.filters.Variables.VariableName); is undefined. Commented May 8, 2018 at 8:02
  • 1
    i have to say console.log($scope.filters.Variables[0].VariableName); is this possible to push this value and console.log($scope.filters.Variables.VariableName); is not undefined? Commented May 8, 2018 at 8:05

3 Answers 3

1

it's not like that,

$scope.filters = {     
    Sites: [], 
    Regions: [], 
    Devices : [], 
    Variables : [],      
};
$scope.filters.Variables.push({
    VariableName: 'Total Active Energy'
});
console.log($scope.filters.Variables);
Sign up to request clarification or add additional context in comments.

Comments

0

Look this code briefly

var $scope = {};

$scope.filters = {     
  Sites: [], 
  Regions: [], 
  Devices : [], 
  Variables : [{VariableName:'Total Active Energy'}]      
 };
 
 console.log("showing total araay:");
 console.log($scope.filters.Variables);
 
 console.log("showing item by item");
 $scope.filters.Variables.forEach(function(itm, index) {
  console.log("item " + JSON.stringify(itm) + " is in index " + index)
 });

5 Comments

It is not a solution of angular js
Is the question related to angularJs? It is something about arrays and objects in javascript! @ProjeshPal
1- normally we don't use $scope.variable format in normal JS Unless someone is unawares of AngularJs 2. The post is tagged with angularJs
So you add ng-repeat because of angularJs. right? @ProjeshPal
No. ng-repeat is to demonstrate that after pushing object scope data is working properly in view. And you did not answer how to push object even in JS. You just run a for each and log static object
0

You need to push object to the array

angular.module('myApp', []).controller('namesCtrl', function($scope) {
  $scope.filters = {     
    Sites: [], 
    Regions: [], 
    Devices : [], 
    Variables : []      
   };
   for(var i=0;i<10;i++){
   
   var obj={VariableName:'Total Active Energy'+i};
   $scope.filters.Variables.push(obj);
   }
   angular.forEach($scope.filters.Variables, function(value, key) {
   console.log("item " + JSON.stringify(value) + " is in index " + key)
   });
   
   
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">
  <ul>
      <li ng-repeat="x in filters.Variables">
        VariableName: {{x.VariableName}}
      </li>
  </ul>

</div>

</body>
</html>

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.