How to make a "for" that writes its values to an array?

1

I have this for that will get all data "this.retorno += data.usuarios[i].descr;" and "this.retorno += data.usuarios[i].valor;" wanted to take these values and make an array with them, then get those values stored in the array in another page with javascript.

I thought of something like this:

var myarry = new Array();

for (i = 0; i < this.qtd; i++) {
  if (i == (this.qtd - 1)) {
    this.retorno += data.usuarios[i].descr;
    this.retorno += data.usuarios[i].valor;
  } else {
    //  this.retorno += data.usuarios[i].descr;
    //  this.retorno += data.usuarios[i].valor;

    myarray[0] = this.retorno += data.usuarios[i].descr;
    myarray[1] = this.retorno += data.usuarios[i].descr;
    myarray[2] = this.retorno += data.usuarios[i].descr;
    myarray[3] = this.retorno += data.usuarios[i].descr;
    myarray[4] = this.retorno += data.usuarios[i].descr;

    alert(myarray[0]);
  }
    
asked by anonymous 25.03.2015 / 13:41

1 answer

1

From what I understand about your issue, you need to use .map() in this way you can use an initial array and redo your content.

I also see in your code that you want to treat the last element differently, you can use .pop() that returns the last element of a array and removes it at the same time.

>

Suggestion:

var ultimo = data.usuarios.pop();
this.retorno += ultimo.descr + ultimo.valor;
var myArray = data.usuarios.map(function(obj){
    return this.retorno += data.usuarios[i].descr;
});

If you do not understand the answer or need an example, add more code and sample data in the question so I can help you more.

    
25.03.2015 / 13:51