Comment ajouter une paire clé/valeur à un objet JavaScript

Mots clés : javascriptobject-literaljavascript

meilleur 5 Réponses Comment ajouter une paire clé/valeur à un objet JavaScript

vote vote

90

var obj = {     key1: value1,     key2: value2 }; 
obj.key3 = "value3"; 
obj["key3"] = "value3"; 
var getProperty = function (propertyName) {     return obj[propertyName]; };  getProperty("key1"); getProperty("key2"); getProperty("key3"); 
var arr = []; 
var arr = new Array(); 
vote vote

80

var obj = {key1: "value1", key2: "value2"}; Object.assign(obj, {key3: "value3"});  document.body.innerHTML = JSON.stringify(obj);
obj = {...obj, ...pair}; 
var obj = {key1: "value1", key2: "value2"}; var pair = {key3: "value3"}; obj = {...obj, ...pair};  document.body.innerHTML = JSON.stringify(obj);
obj += {key3: "value3"}; 
vote vote

73

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"}  
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} 
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"} 
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"} 
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} 
vote vote

63

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

60

var o = {      key1: true,      key2: 3,  };    var log= (s,f)=> console.log(`${s} --> ${JSON.stringify(f({...o}))}`);        function MA(obj) {    obj.key3 = "abc";    return obj;  }    function MB(obj) {    obj['key3'] = "abc";    return obj;  }    function MC(obj) {    Object.assign(obj, {key3:'abc'});    return obj;  }    function MD(obj) {    Object.defineProperty(obj, 'key3', {      value: "abc",       // undefined by default      enumerable: true,      // false by default      configurable: true,    // false by default      writable: true         // false by default    });    return obj;  }    function IA(obj) {    return {...obj, key3:'abc'};  }    function IB(obj) {    return Object.assign({key3:'abc'}, obj);  }    function IC(obj) {    let ob= JSON.parse(JSON.stringify(obj))    ob.key3 = 'abc';    return ob;  }      function ID(obj) {  	let ob= Object.fromEntries(Object.entries(obj));    ob.key3 = 'abc';    return ob;  }    function IE(obj) {  	return Object.fromEntries(Object.entries(obj).concat([['key3','abc']]))  }        log('MA',MA);  log('MB',MB);  log('MC',MC);  log('MD',MD);  log('IA',IA);  log('IB',IB);  log('IC',IC);  log('ID',ID);  log('IE',IE);
This snippet only presents code - it not perform tests itself!

Questions similaires