Object with list of objects within Javascript

Asked

Viewed 1,771 times

2

I am using angular to make a form that contains multiple checkboxs (using angular-material), this form sends the values to the controler, which are received as follows: inserir a descrição da imagem aqui

I need to take this data (all objects contain the same fields) and turn it into a JSON. Does anyone know how this can be done?

Code of my controller:

vm.cadastraGrupo = function (dados){
    vm.disabled = true;
    console.log(dados);
}

My HTML:

<form name="userForm" ng-model="vm.data" ng-submit="vm.cadastraGrupo(vm.data)">
                        <md-input-container class="col-xs-12 md-block">
                            <label>Nome</label>
                            <input ng-model="vm.data.nome_grupo_menu" required="">
                        </md-input-container>
                        <div class="col-md-3 col-sm-6 col-xs-12">
                            <label class="label_tinyMCE left_100">Leitura</label>
                            <md-checkbox aria-label="Select All"
                                         ng-checked="vm.isCheckedLeitura()"
                                         md-indeterminate="vm.isIndeterminateLeitura()"
                                         ng-click="vm.toggleAllLeitura()"> Selecionar tudo
                            </md-checkbox>
                            <div ng-repeat="menus in vm.respostaListaMenusCadastrados">
                                <div style="display: none;">{{vm.data[menus.id_menu].id_menu_rel_permissao = menus.id_menu}}</div>
                                <input type="hidden" ng-model="vm.data[menus.id_menu].id_menu_rel_permissao">
                                <md-checkbox ng-checked="vm.existsLeitura(menus, vm.selectedLeitura)" aria-label="menus {{$index}}" ng-click="vm.toggleLeitura(menus, vm.selectedLeitura)" style="width: 100%;" ng-model="vm.data[menus.id_menu].read_permissao">{{menus.nome_menu_rel}}</md-checkbox>
                            </div>
                        </div>
</form>
  • var conteudoJson = angular.toJson(variavel); ?

  • I think the problem is the numbering that is, for example, "3":{"id_menu_rel_permissao":"3","read_permissao":true}, I would have to take that first 3, in case I would need to transform that variable to something like: var potato = [{'id': 3}];

  • This is the name of the property. You need to iterate over the items and transform, for example, into a collection/array.

  • Yes, I thought about creating an empty array and pushing it, only the problem is that I don’t have the length of the variable I get from HTML and therefore I have no way of knowing when to stop

  • 2

    for(var propriedade in objeto) {};

  • Hello, you can comment on this as an answer for me to give as sure, as this was the right solution. Thank you

  • Bernardo, I only provided the parts, you implemented the solution. = ) Describe your implementation, and mark it as correct.

Show 2 more comments

1 answer

2


With the help of Onosendai, I was able to solve my problem as follows: I receive the data by the controller normally, create a variable in the json format and then make a loop for that iterege over the objects to save them in the json variable

vm.cadastraGrupo = function (dados){
        var data = 
            [
                {
                    'nome_grupo_menu' : dados.nome_grupo_menu,
                    'authenticator' : '',
                    'permissoes' : []
                }
            ]
        for(var i in dados) {
            data[0].permissoes.push({'read_permissao' : dados[i].read_permissao, 'id_menu_rel_permissao' : dados[i].id_menu_rel_permissao});
        }
}

Browser other questions tagged

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