简体   繁体   中英

Working with arrays inside javascript objects and using the array values dynamically

I have this code. I want to set objects dynamically with the value of an array inside other object

var object1 = {repetidos : {}};
var object2 = {nombre: 'Example', precio: 'Example 2', etc...};

//Object with the array
var carga = {
    repetidos : ['nombre','precio']
}

// custom function to set objects... I don't want to return nothing
cargaDatos(object1,object2,carga);

// Here is the function that i'm trying to code
cargaDatos = function(object1,object2,carga){

    // what can i do here?. I have tried to use for loops
    for(var key in carga){
        for(var key2 in key){
           //Here I have tried many things
        }
    }
}
//////////////////////////////////////////////////////////
// I want set something like this inside the function
object1.repetidos.nombre = object2.nombre;  // nombre is variable
object1.repetidos.precio = object2.precio;  // precio is variable
etc ....

What can I do to set the objects like above with variable values?

You can use the [] notation of objects.

 var object1 = { repetidos: {} }; var object2 = { nombre: 'Example', precio: 'Example 2' }; //Object with the array var carga = { repetidos: ['nombre', 'precio'] }; // Here is the function that i'm trying to code var cargaDatos = function(object1, object2, carga) { // what can i do here?. I have tried to use for loops for (var key in carga) { for (var key2 in carga[key]) { // since carga[key] is an array key2 refers to the index var actualKey2 = carga[key][key2]; object1[key][actualKey2] = object2[actualKey2]; } } } // custom function to set objects... I don't want to return nothing cargaDatos(object1, object2, carga); //test console.log(object1); 

You need to use bracket notation to dynamically access the objects elements:

cargaDatos(object1,object2,carga);

function cargaDatos(object1,object2,carga){
    for(var key in carga){
        for(var key2 in key){
           object1[key][carga[key][key2]] = object2[carga[key][key2]];
        }
    }
}
console.log(object1);

Also note, that i changed var cargaDatos = function.... to function cargaDatos because otherwise you need to declare var cargaDatos before you use it. If you just use function cargaDatos it doesn't matter if you declare it before or after you use it.

NOTE: if you want to dynamically fill the entire object1 you have to ensure that the element key exists on object1 :

for(var key in carga){
    for(var key2 in key){
       if(!object1[key])object1[key] = {};
       object1[key][carga[key][key2]] = object2[carga[key][key2]];
    }
}

The main thing to understand is you can access object properties with dynamic keys by obj[key] . To traverse the array you can also use foreach..

cargaDatos = function(object1,object2,carga){
    for(var key in carga){
        carga[key].forEach(function(x){
            object1[key][x] = object2[x];
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM