0
I would like to ask a question about a problem I’m having (I apologize if somewhere has the answer, is that I haven’t found and so I’m posting).
Introducing
I want to display some chekcBoxes already marked as I bring from the database, so far so good, I use Angularjs and get it easy! Through a ng-repeat I display a list of choices, and within each item on that list I have another list of attributes that comes from the database:
var listaDeEscolhas = [{id: 0, atributos: []},
{id: 1, atributos: [{tipo:'visivel'}]},
{id: 2, atributos: []},
{id: 3, atributos: [{tipo:'visivel'}]}];
For the view of checkboxes, I have another list with possible attributes in which the user can choose which one he wants to record in this list of choices:
var possiveisAtributos = [
{description: 'visivel', id: 10},
{description: 'editavel', id: 13}
];
When I bring this information from the bank, I keep this list of Possible contributions on another list called attribute within each item of listDescolhas to then execute another *ng-repeat in this list and actually display to the user. In this list I will display to the user (attributeParaMostrar) I need to set an attribute named Selected with true so that the checkbox is marked as the attribute has already come from the bank, and false otherwise. Then I do the following:
for (var x = 0; x < listaDeEscolhas.length; x++) {
listaDeEscolhas[x].atributosParaMostrar = possiveisAtributos;
for (var y = 0; y < listaDeEscolhas[x].atributos.length; y++) {
for (var z = 0; z < listaDeEscolhas[x].atributosParaMostrar.length; z++) {
if ((listaDeEscolhas[x].atributos[y].tipo==listaDeEscolhas[x].atributosParaMostrar[z].description)){
listaDeEscolhas[x].atributosParaMostrar[z].selected = true;
} else {
listaDeEscolhas[x].atributosParaMostrar[z].selected = false;
}
}
}
}
As quoted above, I keep a copy of the list Possible contributions in each listDescolhas generating attribute, then I check if any of the items from attribute is contained in attributes of listDescolhas and then put true in the Selected or false of each item of attribute
Problem
After running the above code all lists attribute of each list of choices are with Selected equal to true. I have done and re-read this in several ways but without success, I believe that javascript is passing by reference the list of possible attributes instead of assigning it to the list of attributes to show. I apologise again if something like this is already elsewhere and would like some help/analysis on the situation presented.
Do
=
between arrays is to say that they are the same array. This is in fact always by reference. There are already questions with the same problem and so this will be a duplicate. See arrays equality in Java Script and How can I make a copy of an array without reference?.– Isac
Take a look also at this question that this subject is discussed there: https://answall.com/questions/286125/como-fazer-passagem-por-valor-em-javascript/
– Ricardo Pontual