Javascript - Take for result and put each result inside an array

Asked

Viewed 151 times

0

Hello, someone can help me in this matter. I would like to place each result inside my new array.

Example, my for generates the result of 9 Array.

success: function(res){

        for(i in res){
            var dados = [
                [res[i].nome, res[i].sobrenome]
            ];
            console.log(pos);
        }
    }

inserir a descrição da imagem aqui

I would like each space of my new array to be filled with each output of my for, result 1 in space 0 of the new array, result 2 in space 1 of the new array etc....

var result = new Array('','','','','','','','','',);
console.log(result);

inserir a descrição da imagem aqui

I did not find a way to do this in Javascript, if anyone can help me I appreciate.

1 answer

0


You can try like this

success: function(res){
        let dados = [];
        for(let i in res){
            dados.push(
                [res[i].nome, res[i].sobrenome]
            );
        }
        console.log(dados);
    }

Bonus

There are other easier forms to do this with javascript, follow an example:

success: function(res){
    let dados = res.map(
        item => [item.nome, item.sobrenome]);
    console.log(dados);
}
  • That’s exactly it, Thank you very much. I need to study about push and vlw map.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.