How to start from the second property of an object in javascript?

0

I have the following method:

 $http({
      method: 'GET',
      url: '../ws/parqueos'
   }).then(function (success){
            $scope.parqueos = success.data.records;        
            for($scope.parqueosDisponibles in  $scope.parqueos)
            {
             console.log($scope.parqueos[$scope.parqueosDisponibles]);
            }

   },function (error){

   });

variable $scope.parqueos = success.data.records;

stores what comes in the next JSON, exactly in the property "records":

{
  "message": "Consulta Exitosa: Parqueos",
  "result": true,
  "records": {
    "id_info": 2,
    "estac1": 1,
    "estac2": 1,
    "estac3": 1,
    "estac4": 1,
    "estac5": 0,
    "estac6": 0,
    "estac7": 0,
    "estac8": 0,
    "estac9": 1
  }
}

It turns out that I have to perform a counter for the estac1, estac2, estac3, estac4, estac5, estac6, estac6, estac8, estac8, estac8 properties, when these have value 1, I must add up in order to obtain a total of available parking spaces, but not I must take into account the property of the records object, "id_info": 2,

for them I'm using this structure:

for($scope.parqueosDisponibles in  $scope.parqueos)
{
  console.log($scope.parqueos[$scope.parqueosDisponibles]);
}

but it goes through all the values of the object. How can I start from the second property of object records ? or any other solution to my problem?

    
asked by JG_GJ 10.11.2017 в 06:06
source

2 answers

1

You can simply base yourself on the index = 1

var x = {
  "id_info": 2,
  "estac1": 1,
  "estac2": 1,
  "estac3": 1,
  "estac4": 1,
  "estac5": 0,
  "estac6": 0,
  "estac7": 0,
  "estac8": 0,
  "estac9": 1
};

var arrayX = Object.values(x);
var suma = 0;
for (var i = 1; i < arrayX.length; i++) {
  if (arrayX[i]) {
    suma++;
  }
}
console.log("SUMA DESDE EL SEGUNDO ELEMENTO", suma);
    
answered by 10.11.2017 / 18:39
source
0

At first glance you could do the following:

Define a variable of 'control', example:

$http({
    method: 'GET',
    url: '../ws/parqueos'
}).then(function (success){
    //Definimos la variable de 'control'
    var control = false;
    $scope.parqueos = success.data.records;        

        for($scope.parqueosDisponibles in  $scope.parqueos){
            if(control == true){
                console.log($scope.parqueos[$scope.parqueosDisponibles]);
            }
            else{
                control = true;
            }
        }

    },function (error){

});

I hope and it works for you.

    
answered by 10.11.2017 в 08:19