簡體   English   中英

JavaScript從另一個對象更新對象值

[英]JavaScript update Object value from another object

我有兩個對象,一個用於更新另一個,類似於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”,則轉到另一個對象並檢查test1屬性,然后用新值替換currentObject

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

對currentObject attribute3進行更新需要使用updateObject屬性作為參考; 其中currentObject attribute1 =“ test1”應該從updateObject test1復制數據,依此類推:

最終值應類似於:

const currentObject = {
    myObject : [
      {
        'attribute1':'foo1',
        'attribute2':'bar1',
        'attribute3':'newtest1'
      },
      {
        'attribute1':'foo2',
        'attribute2':'bar2',
        'attribute3':'newtest2'
      },
      {
       'attribute1':'foo3',
       'attribute2':'bar3',
       'attribute3':'newtest3'
      }
   ]
}

您可以使用forEachObject.entries

這里的主意是

  • 首先循環遍歷currentObject myObject數組中的每個元素
  • 現在,就像在您的結構中一樣,您具有currentObject值作為updateObject key ,因此我們通過updateObject.myObject[value]檢查是否存在
  • 如果是他們,我們更新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) 

我們可以使用Array.reduce並在updateObject.myObject搜索當前元素的( eleattribute3屬性。

如果存在,則使用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)); 

這變成了具有最新JavaScript語言功能的單行代碼:

 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); 

...而且您還將獲得不變性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM