ThomasReggi
ThomasReggi

Reputation: 59345

Set variable object keys

In the example below is there a way to set one or more of the object keys for instance "value" to a variable? I would like to do this while keeping the JOSN object intact.

var ex = { 
  "value": 4,
  "amount": 1341,
  "data": "tree",
  "animal": "mouse"
}

I know that it is possible to do this.

var value = "value";
var ex = {};
ex[value] = 4;
ex.amount = 1341;
ex.data = "tree";
ex.animal = "mouse";

This is tearing the whole object into pieces and is much harder to manage.

Upvotes: 0

Views: 130

Answers (4)

KooiInc
KooiInc

Reputation: 122888

You could extend the Object.prototype:

Object.prototype.append = Object.prototype.append || function (key,val){
   this[key] = val; return this;
};

to be able to do something like

var value = 'value'
   ,animal = 'animal' 
   ,ex = { 
          "amount": 1341,
           "data": "tree",
         }.append(value,4)
          .append(animal,'mouse');

The append method could be more comprehensive:

Object.prototype.append = Object.prototype.append || function(){
   var args = [].slice.call(arguments);
   if (args.length===1 && /object/i.test(args[0].constructor)){
    for (var l in args[0]){
      if (args[0].hasOwnProperty(l))
       this[l] = args[0][l];
    }
   }
   if (args.length===2){ this[args[0]] = args[1]; }
   if (args.length>2 && args.length%2<1){
    for (var i=0;i<args.length;i+=2){
      this[args[i]] = args[i+1];
    }
   }
   return this;
};

Now you can:

var value = 'value'
   ,animal = 'animal'
   ,amount = 'amount'
   ,data = 'data'
   ,ex = {}.append(amount,1341,data,'tree',value,4,animal,'mouse');

See also this page (on extending native JS-objects)

Upvotes: 1

jfriend00
jfriend00

Reputation: 707158

If you're just trying to save some syntax typing or have a little more freedom to initial objects using property names in a variable, you can create a function that will take a variable number of arguments and interpret each successive pair of arguments as a key and value:

function setProps(obj, key, value /* key2, value2, etc... */) {
    for (var i = 1; i < arguments.length; i+=2){
        obj[arguments[i]] = arguments[i+1];
    }
}

var ex = {};
setProps(ex, "value", 4, "amount", 1341, "data", "tree", "animal", "mouse");

Any key value passed can be either a string or a value from a variable.

There is no way to define statically declared javascript data structures that use property names from a variable. You have to execute code to do that.

Upvotes: 0

RAKESH HOLKAR
RAKESH HOLKAR

Reputation: 2197

It is easy way....

 var ex = { 
  "value": 4,
  "amount": 1341,
  "data": "tree",
  "animal": "mouse"
}

var valList = [['4','1341','tree','mouse'],['5','1341','tree','mouse']];
var objList=[];
for (var i =0; i<valList.length; i++)
{
     j=0;
    for(var o in ex)
    {
    ex[o] = valList[i][j]; 
        j++;
    }
      objList[i]= ex;
}

It will enable to make bunch of object

Upvotes: 0

Ry-
Ry-

Reputation: 224855

Nope, that's not possible. You need to do it the second way, or a mixture of the two:

var ex = {
  "amount": 1341,
  "data": "tree",
  "animal": "mouse"
};

ex[value] = 4;

Upvotes: 3

Related Questions