简体   繁体   中英

how to create variables from key value object pair in javascript

I have an object like this:

Object {geom: "-116.2, 35.6", hazard_c_5: "0.970118393901, 0.904556039223", hazard_c_6: "0.970118393901, 0.904556039223", iml: "0.005, 0.007", pga_mean: "0.987965748334, 0.958177134411"

How can I create a variable for each key and set its value to the keys respective value?

I was trying this:

for(i in data) {
    console.log(i, data[i]);
}

But it's not clear to me how to set i to a variable and data.[i] to the variables value?

I am after:

var geom = "-116.2, 35.6";
var hazard_c_5 = "0.970118393901, 0.904556039223";
...

EDIT

I should also mention that the number of key value pairs in data will change

There is the with statement, which does exactly that.

var data = {
    geom: "-116.2, 35.6",
    hazard_c_5: "0.970118393901, 0.904556039223",
    hazard_c_6: "0.970118393901, 0.904556039223",
    iml: "0.005, 0.007",
    pga_mean: "0.987965748334, 0.958177134411"
}

with(data) {
    console.log(geom, iml);
}

Please note, using with stops the JS Engine from making use of several optimizations and there is usually a better way of doing things than that.

As long as you want to make global variables, you can just create them as properties in the window object:

for(i in data) {
  window[i] = data[i];
}

Now you can use them just as you would use global variables. The only practical difference between window properties and global variables is that window properties can be deleted.

Try this:

(function (context) {
    var data = {
        geom: "-116.2, 35.6",
        hazard_c_5: "0.970118393901, 0.904556039223",
        hazard_c_6: "0.970118393901, 0.904556039223",
        iml: "0.005, 0.007",
        pga_mean: "0.987965748334, 0.958177134411"
    };
    for(var i in data) {
         context[i] = data[i];
    };
    alert(geom);
})(this)

The following will do what you asked for, but why don't you use the object itself? :/

var myObject = {
    x: 1,
    y: 2,
    z: 3
};

for( i in myObject ) {
    this[i] = myObject[i];
    console.log( this[i] );
}

Why would you EVER do this?

This is simply wrong.

Of course you can (but shouldn't) use eval like `eval("var geom = -116.2, 35.6;") but trust me, you don't need this.

I hope you know that you can access the geom value by using obj.geom .

But you ask me: Why I shouldn't?

  • Performance : This will slow down your app. This will show you how much improvements Hugo Bonacci got by simply getting rid of these evals.

  • Security : An experienced developer could potentially inject code into your application. This can be harmful in several ways.

  • Readabilty and debugging : Where are these variables coming from? Why creating them using strings? And, what this eval function does?

That's it. But you can (shouldn't) use eval. But don't use eval.

edit: before negative voting this answer please RTFA , I suggest to NOT use evil.

You can do evil eval:

for (var i in obj) {
    eval("var " + i + " = " + obj[i] + ";");
}

but it's a bad bad bad practice, so you should avoid as much as possible. Instead use the property of the object:

var geom = obj.geom;

but if the problem is that you don't know the properties of an object then you can access them using square brackets:

var foo = obj["geom"];

where "geom" is a string with the name of the property, ie:

var key = "geom"
//later in the code
var foo = obj[key]

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