第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何動態(tài)合并兩個JavaScript對象的屬性?

如何動態(tài)合并兩個JavaScript對象的屬性?

森欄 2019-05-22 14:27:26
如何動態(tài)合并兩個JavaScript對象的屬性?我需要能夠在運行時合并兩個(非常簡單的)JavaScript對象。例如,我想:var obj1 = { food: 'pizza', car: 'ford' }var obj2 = { animal: 'dog' }obj1.merge(obj2);//obj1 now has three properties: food, car, and animal有沒有人有這個腳本或知道內(nèi)置的方法來做到這一點?我不需要遞歸,我不需要合并函數(shù),只需要平面對象上的方法。
查看完整描述

5 回答

?
MM們

TA貢獻1886條經(jīng)驗 獲得超2個贊

ECMAScript 2018標準方法

你會使用對象傳播

let merged = {...obj1, ...obj2};/** There's no limit to the number of objects you can merge.
 *  Later properties overwrite earlier properties with the same name. */const allRules = {...obj1, ...obj2, ...obj3};

ECMAScript 2015(ES6)標準方法

/* For the case in question, you would do: */Object.assign(obj1, obj2);/** There's no limit to the number of objects you can merge.
 *  All objects get merged into the first object. 
 *  Only the object in the first argument is mutated and returned.
 *  Later properties overwrite earlier properties with the same name. */const allRules = Object.assign({}, obj1, obj2, obj3, etc);

(參見MDN JavaScript參考


ES5和早期的方法

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

請注意,這會簡單地添加的所有屬性obj2,以obj1這可能不是你想要什么,如果你仍然想使用未修改obj1。

如果你使用的是一個遍布原型的框架,那么你必須得到更好的支票hasOwnProperty,但是這些代碼適用于99%的情況。

功能示例:

/**
 * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
 * @param obj1
 * @param obj2
 * @returns obj3 a new object based on obj1 and obj2
 */function merge_options(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
    return obj3;}


查看完整回答
反對 回復 2019-05-22
?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

和諧的ECMAScript 2015年(ES6)規(guī)定Object.assign,將做到這一點。

Object.assign(obj1, obj2);

當前的瀏覽器支持正在變得越來越好,但如果您正在為沒有支持的瀏覽器進行開發(fā),則可以使用polyfill。


查看完整回答
反對 回復 2019-05-22
?
汪汪一只貓

TA貢獻1898條經(jīng)驗 獲得超8個贊

我用google搜索代碼來合并對象屬性,最后來到這里。但是由于沒有任何遞歸合并代碼,我自己寫了。(也許jQuery擴展是遞歸BTW?)無論如何,希望其他人也會覺得它很有用。


(現(xiàn)在代碼不使用Object.prototype:)


/*

* Recursively merge properties of two objects 

*/

function MergeRecursive(obj1, obj2) {


  for (var p in obj2) {

    try {

      // Property in destination object set; update its value.

      if ( obj2[p].constructor==Object ) {

        obj1[p] = MergeRecursive(obj1[p], obj2[p]);


      } else {

        obj1[p] = obj2[p];


      }


    } catch(e) {

      // Property in destination object not set; create it and set its value.

      obj1[p] = obj2[p];


    }

  }


  return obj1;

}

一個例子

o1 = {  a : 1,

        b : 2,

        c : {

          ca : 1,

          cb : 2,

          cc : {

            cca : 100,

            ccb : 200 } } };


o2 = {  a : 10,

        c : {

          ca : 10,

          cb : 20, 

          cc : {

            cca : 101,

            ccb : 202 } } };


o3 = MergeRecursive(o1, o2);

生成對象o3之類的


o3 = {  a : 10,

        b : 2,

        c : {

          ca : 10,

          cb : 20,

          cc : { 

            cca : 101,

            ccb : 202 } } };


查看完整回答
反對 回復 2019-05-22
?
MMTTMM

TA貢獻1869條經(jīng)驗 獲得超4個贊

請注意,underscore.jsextend-method在單行中執(zhí)行此操作:

_.extend({name : 'moe'}, {age : 50});
=> {name : 'moe', age : 50}


查看完整回答
反對 回復 2019-05-22
  • 5 回答
  • 0 關注
  • 1840 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號