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

前端 未结 24 3312
情歌与酒
情歌与酒 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:22

    I have grown fond of the LoDash / Underscore when writing larger projects.

    Adding by obj['key'] or obj.key are all solid pure JavaScript answers. However both of LoDash and Underscore libraries do provide many additional convenient functions when working with Objects and Arrays in general.

    .push() is for Arrays, not for objects.

    Depending what you are looking for, there are two specific functions that may be nice to utilize and give functionality similar to the the feel of arr.push(). For more info check the docs, they have some great examples there.

    _.merge (Lodash only)

    The second object will overwrite or add to the base object. undefined values are not copied.

    var obj = {key1: "value1", key2: "value2"};
    var obj2 = {key2:"value4", key3: "value3", key4: undefined};
    _.merge(obj, obj2);
    console.log(obj);
    // → {key1: "value1", key2: "value4", key3: "value3"} 
    

    _.extend / _.assign

    The second object will overwrite or add to the base object. undefined will be copied.

    var obj = {key1: "value1", key2: "value2"};
    var obj2 = {key2:"value4", key3: "value3", key4: undefined};
    _.extend(obj, obj2);
    console.log(obj);
    // → {key1: "value1", key2: "value4", key3: "value3", key4: undefined}
    

    _.defaults

    The second object contains defaults that will be added to base object if they don't exist. undefined values will be copied if key already exists.

    var obj = {key3: "value3", key5: "value5"};
    var obj2 = {key1: "value1", key2:"value2", key3: "valueDefault", key4: "valueDefault", key5: undefined};
    _.defaults(obj, obj2);
    console.log(obj);
    // → {key3: "value3", key5: "value5", key1: "value1", key2: "value2", key4: "valueDefault"}
    

    $.extend

    In addition, it may be worthwhile mentioning jQuery.extend, it functions similar to _.merge and may be a better option if you already are using jQuery.

    The second object will overwrite or add to the base object. undefined values are not copied.

    var obj = {key1: "value1", key2: "value2"};
    var obj2 = {key2:"value4", key3: "value3", key4: undefined};
    $.extend(obj, obj2); 
    console.log(obj);
    // → {key1: "value1", key2: "value4", key3: "value3"}
    

    Object.assign()

    It may be worth mentioning the ES6/ ES2015 Object.assign, it functions similar to _.merge and may be the best option if you already are using an ES6/ES2015 polyfill like Babel if you want to polyfill yourself.

    The second object will overwrite or add to the base object. undefined will be copied.

    var obj = {key1: "value1", key2: "value2"};
    var obj2 = {key2:"value4", key3: "value3", key4: undefined};
    Object.assign(obj, obj2); 
    console.log(obj);
    // → {key1: "value1", key2: "value4", key3: "value3", key4: undefined}
    

提交回复
热议问题