Access attributes of an object within an array in JS

1

I create an object with 3 parameters, and insert it into an array. Now, when I go through the array, I always get undefined , and I've tried several things, but nothing.

This is my code:

//***************************  Arrays ***************************
//El que va a contener todos los votos, el que recorro que dentro tiene los objetos.
var contenedor = [];
var colegiosPrueba = ["Virgen Pilar"];
var partidos = ["PP", "PSOE", "Podemos", "Ciudadanos", "IU"];

//Por cada colegio, coge un voto de cada partido
function aVotar() {
  for (var i = 0; i < colegiosPrueba.length; i++) {
    for (var j = 0; j < partidos.length; j++) {
      var aux = prompt(colegiosPrueba[i] + "--" + partidos[j]);
      voto(colegiosPrueba[i], partidos[j], aux);
      contenedor.push(voto);
    }
  }
}

//Ver array
function verArray() {
  for (var i = 0; i < contenedor.length; i++) {
    var auxi = parseInt((contenedor[i].getColegio));

  }
}

function voto(colegio, partido, numeroVotos) {
  this.colegio = colegio;
  this.partido = partido;
  this.numeroVotos = numeroVotos;

  this.getColegio = function(colegio) {
    return this.votos[colegio];
  }
  this.getPartido = function(partido) {
    return this.votos[partido];
  }
  this.getNumeroVotos = function(numeroVotos) {
    return this.votos[numeroVotos];
  }
}

aVotar();
console.log(contenedor);
    
asked by cupax64 31.12.2018 в 22:56
source

1 answer

0

The problem is in these lines of code:

        voto(colegiosPrueba[i], partidos[j], aux);
        contenedor.push(voto);

voto is a function that is used to simulate a class in JavaScript, you are calling it but you are not saving the result anywhere. Then, you make a push of voto , which is still the function ... or rather, the result of a function that does not return anything. That's why you're going to have undefined .

Call the function using new (as if it were the constructor of a class) and save the result in a variable. Then do push of that variable in the container array to save the value. Something like this:

            var votoValor = new voto(colegiosPrueba[i], partidos[j], aux);
            contenedor.push(votoValor);

That way you'll save what you want instead of undefined as you can see in this example:

  

Note: this solution only focuses on solving the part of receiving undefined , the code presents other errors that I have not solved , because it would lack information to know what or how should be resolved. For example: the function verArray is incomplete and incorrectly calls a method of the object ( auxi will always be NaN because it is doing parseInt of a function and not the result that returns that function because it is missing the parentheses , but even with parentheses, the function has problems: what is this.votos ? What parameter are you going through?)

//***************************  Arrays ***************************
//El que va a contener todos los votos, el que recorro que dentro tiene los objetos.
var contenedor = [];
var colegiosPrueba = ["Virgen Pilar"];
var partidos = ["PP", "PSOE", "Podemos", "Ciudadanos", "IU"];

//Por cada colegio, coge un voto de cada partido
function aVotar() {
    for (var i = 0; i < colegiosPrueba.length; i++) {
        for (var j = 0; j < partidos.length; j++) {
            var aux = prompt(colegiosPrueba[i] + "--" + partidos[j]);
            const votoValor = new voto(colegiosPrueba[i], partidos[j], aux);
            contenedor.push(votoValor);
        }
    }
}

//Ver array
function verArray() {
    for( var i = 0; i<contenedor.length;i++){
        var auxi = parseInt((contenedor[i].getColegio));

    }
}

function voto(colegio, partido, numeroVotos) {
    this.colegio = colegio;
    this.partido = partido;
    this.numeroVotos = numeroVotos;

    this.getColegio = function (colegio) {
        return this.votos[colegio];
    }
    this.getPartido = function (partido) {
        return this.votos[partido];
    }
    this.getNumeroVotos = function (numeroVotos) {
        return this.votos[numeroVotos];
    }
}

aVotar();
console.log(contenedor);
verArray();
    
answered by 01.01.2019 в 02:48