6

I am trying to insert an object formatted like this:

$scope.newObj = {1: "N/A", 2: "KO", 3: "OK", 4: "OK", 5: "OK", 15: "N/A", 19: "OK"} 

I tried using the following for loop:

var objt = $scope.newObject;
      console.log($scope.newObject[0]) // undefined
for(i=0;i<$scope.newObj.length;i++)
{
   $http.post("insert?etat="+$scope.newObject[0]+"&id="+Object.keys(objt)) 
}

But it doesn't seem to work. I am getting undefined everywhere. Does anyone have an idea on how to retrieve the data from that object row by row then insert the values to the service?

scotthenninger
  • 3,921
  • 1
  • 15
  • 24
Kamel Mili
  • 1,374
  • 3
  • 19
  • 43
  • Can you show what a sample `$http.post` would look like? – Sajal May 30 '16 at 16:28
  • 2
    Are `newObj` and `newObject` different things or is that a typo? – Ouroborus May 30 '16 at 16:29
  • 1
    This is really confusing since you have mismatched variable names , and an array loop but no array is mentioned. Please see [ask] and provide all relevant details. It's not clear what you are working with or what expected results are – charlietfl May 30 '16 at 16:35

1 Answers1

2

newObj is an object rather than a array. You have tried to use it like a array. So just a iteration code needs to modify:

Use the following code:

var objt = $scope.newObject;
for(var key in objt)
{
   $http.post("insert?etat="+objt[key]+"&id="+key) 
}
abhinsit
  • 3,214
  • 4
  • 21
  • 26
  • 1
    best to use `if(objt.hasOwnProperty(key)` or loop over `Object.keys(objt)` However I don't think this is what OP is trying to do – charlietfl May 30 '16 at 16:33