【问题标题】:JavaScript update Object value from another objectJavaScript 从另一个对象更新对象值
【发布时间】:2019-03-27 16:41:00
【问题描述】:

我有两个对象,一个用于更新另一个,类似于 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 属性进行更新需要使用 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'
      }
   ]
}

【问题讨论】:

  • 你有一个代码 sn-p 你试图实现它吗?

标签: javascript object ecmascript-6


【解决方案1】:

您可以使用forEachObject.entries

这里的想法是

  • 首先循环遍历myObject 数组currentObject 中的每个元素
  • 现在,在您的结构中,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)

【讨论】:

  • 我花了一些时间才完全理解逻辑并最终产生疑问,如果我想更改“e [key]”以更新 currentObject 中另一个属性的值,可以吗?仅通过更新这行代码来完成? e[key] = updateObject.myObject[value],感谢代码,这是提高我技能的好逻辑。
【解决方案2】:

我们可以使用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));

【讨论】:

  • 嘿,太好了,你能帮我理解这行代码吗:finalObj = {[Object.keys(currentObject)[0]] : out };
  • @bernlt 实际上是在finalObj 输出中添加myObject 键。所以Object.keys 将返回currentObject 中的键数组,这将是[myObject],但我想要字符串值,所以我需要从数组中获取第'0'个索引。
【解决方案3】:

这变成了具有最新 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);

...而且您还可以获得不变性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2021-11-27
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    相关资源
    最近更新 更多