How can I add a key/value pair to a JavaScript object?

前端 未结 24 3302
情歌与酒
情歌与酒 2020-11-21 07:01

Here is my object literal:

var obj = {key1: value1, key2: value2};

How can I add field key3 with value3 to the ob

相关标签:
24条回答
  • 2020-11-21 07:40
    arr.key3 = value3;
    

    because your arr is not really an array... It's a prototype object. The real array would be:

    var arr = [{key1: value1}, {key2: value2}];
    

    but it's still not right. It should actually be:

    var arr = [{key: key1, value: value1}, {key: key2, value: value2}];
    
    0 讨论(0)
  • 2020-11-21 07:41

    You could use either of these (provided key3 is the acutal key you want to use)

    arr[ 'key3' ] = value3;
    

    or

    arr.key3 = value3;
    

    If key3 is a variable, then you should do:

    var key3 = 'a_key';
    var value3 = 3;
    arr[ key3 ] = value3;
    

    After this, requesting arr.a_key would return the value of value3, a literal 3.

    0 讨论(0)
  • 2020-11-21 07:41

    I know there is already an accepted answer for this but I thought I'd document my idea somewhere. Please [people] feel free to poke holes in this idea, as I'm not sure if it is the best solution... but I just put this together a few minutes ago:

    Object.prototype.push = function( key, value ){
       this[ key ] = value;
       return this;
    }
    

    You would utilize it in this way:

    var obj = {key1: value1, key2: value2};
    obj.push( "key3", "value3" );
    

    Since, the prototype function is returning this you can continue to chain .push's to the end of your obj variable: obj.push(...).push(...).push(...);

    Another feature is that you can pass an array or another object as the value in the push function arguments. See my fiddle for a working example: http://jsfiddle.net/7tEme/

    0 讨论(0)
  • 2020-11-21 07:41

    A short and elegant way in next Javascript specification (candidate stage 3) is:

    obj = { ... obj, ... { key3 : value3 } }

    A deeper discussion can be found in Object spread vs Object.assign and on Dr. Axel Rauschmayers site.

    It works already in node.js since release 8.6.0.

    Vivaldi, Chrome, Opera, and Firefox in up to date releases know this feature also, but Mirosoft don't until today, neither in Internet Explorer nor in Edge.

    0 讨论(0)
  • 2020-11-21 07:42

    There are two ways to add new properties to an object:

    var obj = {
        key1: value1,
        key2: value2
    };
    

    Using dot notation:

    obj.key3 = "value3";
    

    Using square bracket notation:

    obj["key3"] = "value3";
    

    The first form is used when you know the name of the property. The second form is used when the name of the property is dynamically determined. Like in this example:

    var getProperty = function (propertyName) {
        return obj[propertyName];
    };
    
    getProperty("key1");
    getProperty("key2");
    getProperty("key3");
    

    A real JavaScript array can be constructed using either:

    The Array literal notation:

    var arr = [];
    

    The Array constructor notation:

    var arr = new Array();
    
    0 讨论(0)
  • 2020-11-21 07:43

    Your example shows an Object, not an Array. In that case, the preferred way to add a field to an Object is to just assign to it, like so:

    arr.key3 = value3;
    
    0 讨论(0)
提交回复
热议问题