How to create javascript array object inside a FOR?

0

I need to pass an array object as follows, to api:

[paulo = 1, gustavo = 2, amanda = 3,...]

I have the following code:

$scope.salvarHabilidades = function(pro){

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

    var habi = new Array();

    for(var i = 0; i < valores.length; i++){
        console.log(valores[i]);
        habi = [(valores[i].descricao = valores[i].idhabilidade)];
    }
    habi.idusuario = $rootScope.idPro;
    console.log(habi);

}

Because what appears in the console of the line "console.log (values [i])" is this:

Followtheselectionscreen:

    
asked by anonymous 24.05.2017 / 21:04

1 answer

2

You could store objects in array using Array # map :

var habi = [];
var valores = [{
  "idhabilidade": 1,
  "descricao": "Descricao 1"
}, {
  "idhabilidade": 2,
  "descricao": "Descricao 2"
}]

habi = valores.map(function(item) {
  return {
    "nome": item.descricao,
    "valor": item.idhabilidade
  }
})

console.log(habi);
    
24.05.2017 / 21:44