JavaScript 从另一个对象更新对象值



我有两个对象,一个用于更新另一个,类似于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 属性,并将当前对象替换为新值

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

是在当前对象属性 3 上完成的,需要使用更新对象属性作为引用;其中 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

这里的想法是

  • 首先循环遍历myObject currentObject数组中的每个元素
  • 现在就像在你的结构中一样,你的值currentObject key updateObject,所以我们通过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中搜索当前元素的 (ele( attribute3 属性。

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

。你也得到了不变性。

最新更新