How to get the array object data in javascript inside a FOR?

Asked

Viewed 2,196 times

0

I need to get the data from the object.

$scope.salvarHabilidades = function(pro){

    var valores = pro.filter(function(o,i){
        return o.habilidades == true;
    });

    console.log(valores);

    var dados = [];

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

        dados.push(valores[i].idhabilidade);

    }

}

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • You can’t understand what you want to do. You want to create an array of objects in a for?

  • Exactly! I have a javascript FOR and I want to mount an array object inside it

  • Okay, Perai. You just edit the question saying you want to "catch" values. Please decide, I have written two different answers since I opened the post.

  • I want to mount an array object, inside the FOR, taking only the ids, got it? Look at my code, inside the FOR... On the console appears a message saying idhabilidade is undefined.

  • If you had explained it in the question it would be much easier

  • Imagine how big the title would be if I put it all.

  • That’s what it’s for the body of the question.

Show 2 more comments

1 answer

2


Just need to create make one push in the array.

var valores = [{descricao: 'Gesso convencional', habilidades: true, idHabilidade: 1}, {descricao: 'Gesso acartonado', habilidades: true, idHabilidade: 2}];

var dados = [];

for(let elemento of valores){
  dados.push(elemento.idHabilidade);
}

console.log(dados);

Or use the function map (much more elegant, right).

var valores = [{descricao: 'Gesso convencional', habilidades: true, idHabilidade: 1}, {descricao: 'Gesso acartonado', habilidades: true, idHabilidade: 2}];

var dados = valores.map(function(e) { return e.idHabilidade; } );

console.log(dados);

  • I’m doing this, because one or more data, values may come... Since I don’t know how many come, I do a FOR and I put the values in an array object to send to the api!! Did you see the print I put here in the description? It has 2 values two objects coming.

  • Okay, it’s just that in the first version of your question none of this was clear. Anyway, that’s what you need?

  • I’m trying, I’m answering.

  • Dude, what appeared on the console was: [Undefined, Undefined]

  • It is because the H of your code is tiny, in my no.

  • Yes, it worked... Your code was with H and is h kkkkkk. Thanks

  • And if I want to put one more die, beyond the id? kkk

  • 1

    return {idHabilidade: e.idHabilidade, outraCoisa: e.outraCoisa } This returns an object with two properties. (In case you are using the function map).

  • Man, I can’t create the object the way you said it. Looks like this: ["CONVENTIONAL PLASTER", "1", "PLASTER ACARTONADO (DRYWALL)", "2", option: "Cadastra skills", idusuario: 9] And my api does not recognize as object kkkk

Show 4 more comments

Browser other questions tagged

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