2013-05-07 281 views
6

我想向具有現有屬性的現有對象添加多個屬性。每個新屬性有一個比每行更簡潔的方式嗎?在MDN將多個屬性添加到現有的js對象

myObject.name = 'don'; 
myObject.gender = 'male'; 

一切都顯示瞭如何做括號來新的對象,而不是現有對象:https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects

+1

You coul d創建一個接收對象和新對象的函數,枚舉新對象,更新舊對象。這會更簡潔一點。 'update(obj,{name:「don」,gender:「male」})' – 2013-05-07 19:49:56

回答

5

How can I merge properties of two JavaScript objects dynamically?

var obj2 = {name: 'don', gender: 'male'}; 
for (var attrname in myobject) { myobject[attrname] = obj2[attrname]; } 

編輯

要了解如何可以擴展對象使用此功能更清楚一點:

//Extend the protype so you can make calls on the instance 
Object.prototype.merge = function(obj2) { 
    for (var attrname in obj2) { 
     this[attrname] = obj2[attrname]; 
    } 
    //Returning this is optional and certainly up to your implementation. 
    //It allows for nice method chaining. 
    return this; 
}; 
//Append to the object constructor function so you can only make static calls 
Object.merge2 = function(obj1, obj2) { 
    for (var attrname in obj2) { 
     obj1[attrname] = obj2[attrname]; 
    } 
    //Returning obj1 is optional and certainly up to your implementation 
    return obj1; 
}; 

用法:

var myObject1 = { One: "One" }; 
myObject1.merge({ Two: "Two" }).merge({ Three: "Three" }); 
//myObject1 is { One: "One", Two: "Two", Three: "Three", merge: function } 


var myObject2 = Object.merge2({ One: "One" }, { Two: "Two" }); 
Object.merge2(myObject2, { Three: "Three" }); 
//myObject2 is { One: "One", Two: "Two", Three: "Three" } 

注意:您當然可以根據您的需要實行靈活的合併衝突策略。

3

使用jQuery庫

jQuery.extend(myObject, { name : 'don', gender : 'male' }); 
+0

不幸的是不能使用jQuery。 (像主要不幸)雖然這看起來超級乾淨... – 2013-05-07 19:50:27

+0

這個問題沒有標籤[標籤:jQuery]。 – 2013-05-07 19:50:31

+0

僅僅用於擴展對象的jQuery是愚蠢的。 – 2013-05-07 19:50:35

4

有時候,我不喜歡這樣寫道:

var props = { 
    name : 'don', 
    gender : 'male', 
    age : 12, 
    other : false 
}; 
for(var p in props) myObject[p] = props[p]; 
2

在ECMAScript中5就可以使我們的defineProperties

Object.defineProperties(myobject, { 
    name: { 
    value: 'don' 
    }, 
    gender: { 
    value: 'male' 
    } 
}); 
+0

不幸的是,語法非常冗長,甚至比OP希望精簡的原始語言更長。 – 2013-05-07 19:55:21

+0

是的,我想你是對的,當你想防止你的對象發生變異時,這有更多的用處。 嘿嘿,我總是這麼做... = P – JimmyRare 2013-05-07 19:57:52

+0

我同意。這是一個很好的功能,只是笨重。 – 2013-05-07 19:59:13

-1

.push()方法爲我工作在一個自由編碼營類問題。我不知道這在實際應用中是否可行,但它通過了測試。 這裏是很到位:

var myMusic = [ 
    { 
    "artist": "Billy Joel", 
    "title": "Piano Man", 
    "release_year": 1973, 
    "formats": [ 
     "CS", 
     "8T", 
     "LP" ], 
    "gold": true 
    }]; 

這是我進入:

myMusic.push({ 
    "artist": "Metallica", 
    "title": "Ride the Lightning", 
    "release_year" : 1984, 
    "formats": [ 
     "CS", 
     "8T", 
     "LP" ] 
    } // Add record here 
); 

這裏是它顯示的輸出:

[ { 
    "artist": "Billy Joel", 
    "title": "Piano Man", 
    "release_year": 1973, 
    "formats": [ 
     "CS", 
     "8T", 
     "LP" ], 
    "gold": true 
    },{ 
    "artist": "Metallica", 
    "title": "Ride the Lightning", 
    "release_year" : 1984, 
    "formats": [ 
     "CS", 
     "8T", 
     "LP" ] 
    }] 
+0

這意味着作爲一個答案。原始帖子詢問是否有辦法一次向現有對象添加多個事物。 – 2016-07-13 20:34:13

+3

等待,但這是一個額外的對象添加到數組。不是一回事。 – Julix 2016-12-08 00:45:33

3

在ES6/ES2015可以用Object.assign方法

let obj = {key1: true}; 
console.log('old obj: ', obj); 
let newObj = {key2: false, key3: false}; 

Object.assign(obj, newObj); 
console.log('modified obj: ', obj); 
相關問題