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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

JavaScript從另一個(gè)對(duì)象更新對(duì)象值

JavaScript從另一個(gè)對(duì)象更新對(duì)象值

ibeautiful 2021-03-30 09:22:51
我有兩個(gè)對(duì)象,一個(gè)用于更新另一個(gè),類似于ETL Process。const currentObject = {    myObject : [      {        'attribute1':'foo1',        'attribute2':'bar1',        'attribute3':'test1'      },      {        'attribute1':'foo2',        'attribute2':'bar2',        'attribute3':'test2'      },      {       'attribute1':'foo3',       'attribute2':'bar3',       'attribute3':'test3'      },   ]}如果attribute3值為“ test1”,則轉(zhuǎn)到另一個(gè)對(duì)象并檢查test1屬性,然后用新值替換currentObjectconst updateObject = {  myObject : {    'test1':'newtest1',    'test2':'newtest2',    'test3':'newtest3'  }}在currentObject attribute3上完成更新需要使用updateObject屬性作為參考;其中currentObject attribute1 =“ test1”應(yīng)該從updateObject test1復(fù)制數(shù)據(jù),依此類推:最終值應(yīng)類似于:const currentObject = {    myObject : [      {        'attribute1':'foo1',        'attribute2':'bar1',        'attribute3':'newtest1'      },      {        'attribute1':'foo2',        'attribute2':'bar2',        'attribute3':'newtest2'      },      {       'attribute1':'foo3',       'attribute2':'bar3',       'attribute3':'newtest3'      }   ]}
查看完整描述

3 回答

?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以使用forEachObject.entries

這里的主意是

  • 首先循環(huán)遍歷myObject數(shù)組中的每個(gè)元素currentObject

  • 現(xiàn)在,在你的結(jié)構(gòu)你的價(jià)值currentObjectkeyupdateObject,所以我們通過(guò)檢查是否存在updateObject.myObject[value]

  • 如果是他們,我們會(huì)更新,currentObject否則我們將其保持不變

const currentObject = {myObject : [{'attribute1':'foo1','attribute2':'bar1','attribute3':'test1'},{'attribute1':'foo2','attribute2':'bar2','attribute3':'test2'},{'attribute1':'foo3','attribute2':'bar3','attribute3':'test3'},]}

const updateObject = {myObject : {'test1':'newtest1','test2':'newtest2','test3':'newtest3'}}


currentObject.myObject.forEach(e => {


Object.entries(e).forEach(([key,value]) => {

    if(updateObject.myObject[value]){

      e[key] = updateObject.myObject[value]

    }

  })

})


console.log(currentObject)


查看完整回答
反對(duì) 回復(fù) 2021-04-08
?
開(kāi)滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

這樣就形成了具有最新JavaScript語(yǔ)言功能的單行代碼:


const currentObject = {

  myObject: [

    {

      'attribute1': 'foo1',

      'attribute2': 'bar1',

      'attribute3': 'test1'

    },

    {

      'attribute1': 'foo2',

      'attribute2': 'bar2',

      'attribute3': 'test2'

    },

    {

      'attribute1': 'foo3',

      'attribute2': 'bar3',

      'attribute3': 'test3'

    },

  ]

}


const updateObject = {

  myObject: {

    'test1': 'newtest1',

    'test2': 'newtest2',

    'test3': 'newtest3'

  }

}


const result = { myObject: currentObject.myObject.map(o => ({ ...o, ...{ 'attribute3': updateObject.myObject[o.attribute3] } })) };


console.log(result);


查看完整回答
反對(duì) 回復(fù) 2021-04-08
?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

我們可以在中使用Array.reduce和搜索當(dāng)前元素的(ele)attribute3屬性u(píng)pdateObject.myObject。


如果存在,則使用其他中的匹配值對(duì)其進(jìn)行更新,并updateObject.myObject保留舊的:


const currentObject = {myObject : [{'attribute1':'foo1','attribute2':'bar1','attribute3':'test1'},{'attribute1':'foo2','attribute2':'bar2','attribute3':'test2'},{'attribute1':'foo3','attribute2':'bar3','attribute3':'test3'},]};

const updateObject = {myObject : {'test1':'newtest1','test2':'newtest2','test3':'newtest3'}};


function transformObject(currentObject, updateObject){

    const out = currentObject.myObject.reduce((acc, ele) => {

       ele.attribute3 = updateObject.myObject[ele.attribute3] ?

                        updateObject.myObject[ele.attribute3] : 

                        ele.attribute3;

       return acc.concat(ele);

    }, []);

    finalObj = {[Object.keys(currentObject)[0]] : out };

    return finalObj;

}

console.log(transformObject(currentObject, updateObject));


查看完整回答
反對(duì) 回復(fù) 2021-04-08
  • 3 回答
  • 0 關(guān)注
  • 770 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)