Information stored and displayed wrong

0

Hello, I have the following code:

    var marcas = {
        nome: '',
        fipeId: ''    
    };

    var marcasVet = [];
    var select;


$.ajax({
    dataType: "json",
    url: 'http://fipeapi.wipsites.com.br/carros/marcas',

    success: function(data) {

        for (var i = 0; i < data.length; i++) {

            marcas.nome = data[i].name;
            marcas.fipeId = data[i].id;            


            marcasVet[i] = marcas;

            select += '<p> Marca: ' + marcasVet[i].nome + ' Marca id (Fipe): ' + marcasVet[i].fipeId + '</p>';

        }

        $('#info').html(select);
    }

});

The data is stored in the marcasVet[] vector and shown inside a div with id="info"

The problem is that when asking to show the information of marcas.fipeId it only shows the value 120 , at any position of the marcasVet[] vector.

Changing the code $('#info').html(select); to $('#info').html(marcasVet[10].fipeId); or any other position (1,2,3,4 ... 87) it shows only the value 120 .

I would like you to show the value it shows when you change to% var from%.

Does anyone know where the error is?

Thank you

    
asked by anonymous 10.09.2017 / 02:35

1 answer

0

Re-enter the value of the variable marcas into for . And use the push() method to add the object to its marcasVet vector. See:

    
var marcas = {nome: '', fipeId: ''};
var marcasVet = [];

for (var i = 0; i < 5; i++) {
 
  marcas = {nome: '', fipeId: ''};
  
  marcas.nome = "Nome "+i;
  marcas.fipeId = "Fipe "+i;
  
  marcasVet.push(marcas);
}

console.log(marcasVet);
    
10.09.2017 / 05:48