user2403131
user2403131

Reputation: 43

Dynamic variables - How can I get as name and value?

I need to create dynamic variables and I need to get name and value this.

My Code:

console.log(listaY)

$.each(listaY, function(key,val){
    console.log(eval(val.nodeName + "Prop" + "= val.nodeValue"));
    console.log(eval(val.nodeName+"Prop").nodeName);
    console.log(key + ":" + val);
});

Console:

 Object {window: Object}
  window: Object
    childrens: Array[1]
      0: Object
        name: Object
        height: "height"
      __proto__: Object
    __proto__: Object
    length: 1
    __proto__: Array[0]
    height: "auto"
    nome: "Win2"
    width: "auto"
  __proto__: Object
__proto__: Object

view

undefined

0:[object Attr] 

listaY is that DOM object

I need to get the name of the variable and its value from the name. How I can get this?

Solution:

var obj = {}; 
$.each(listaY,function(key,val){ 
    obj[val.nodeName + "Prop"] =  val.nodeValue; 
});

by: @Rocket Hazmat

Upvotes: 0

Views: 126

Answers (1)

gen_Eric
gen_Eric

Reputation: 227270

Instead of using eval, try setting the values of an object.

var obj = {};
$.each(listaY,function(key,val){
    obj[val.nodeName + "Prop"] = val.nodeValue;
});

Upvotes: 1

Related Questions