I have a variable that is an object with 2 nodes:
var jDef = {id: 'hola', valores: [{cod: 1, desc: 'dddd'}, {cod: 2, desc: 'bbbb'}]}
In another part of the program, I do:
var jTemp.valores[1] = $.extend(jDef.valores[1], {selected: true});
This works as I hope.
If I have it this way (function that picks up the list of a BBDD):
var jDef = {id: 'hola', valores: obtenerListaBBDD()};
var jTemp = $.extend({ }, jDef);
jTemp.valores[1] = $.extend(jDef.valores[1], {selected: true});
The new node 'selected' appears in both variables (jDef and jTemp), when I want it to only be included in jTemp.
Can someone clarify the concepts or give me a solution?
I have mounted a jsFiddle link
Greetings,
SeakOink (I have edited question, since it was not accurate)
EDIT:
This is the real code. jCRUD.Datatable [n] .valors_admesos [1] has the same value as jTemp.valors_admesos [1] (with the new node "selected":
var jTemp = $.extend({}, jCRUD.DataTable[n], {data: sValor}); //Subtitueix el node "data" amb el valor calculat
nn = 0;
nnTotal = _.size(jTemp.valors_admesos);
while (nn < nnTotal) {
// Bucle en cas de tenir una llista de valors en un <select>, el qual marcarà com a "selected" el <option> que correspon al valor
if (jTemp.valors_admesos[nn].valor === sValor) {
jTemp.valors_admesos[nn] = $.extend({}, jTemp.valors_admesos[nn], {selected: 'selected'}); // Marco el valor del camp com a seleccionat a dins la llista
}
nn++;
}