有条件地更新对象

有条件地更新对象

本文介绍了在Javascript中,如何有条件地更新对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过并且想知道是否有一种方法可以使用javascript来编写一个函数,该函数可以通过使用具有新值的另一个对象来有条件地更新对象的所有属性?

I've seen this post and was wondering if there was a way of using javascript to write a function that could conditionally update all properties of an object by using another object with the new values?

说我有这个对象:

let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };

我想使用另一个对象传递的属性更新其值,如下所示:

And I want to update its values using the properties passed by another object like this:

let newObj = {test2: 'newValue2'}

在这种情况下,我只想更新属性test2。如果我知道我要更新的属性并且我事先知道原始属性,我将使用:

In this case I just want to update property "test2". If I know the properties I want to update and I know the original properties beforehand, I'll use:

oldObj = {
  ...(newObj.test1) && {test1: newObj.test1} || {test1: oldObj.test1},
  ...(newObj.test2) && {test2: newObj.test2} || {test2: oldObj.test2},
  ...(newObj.test3) && {test3: newObj.test3} || {test3: oldObj.test3},
};

意思是我只会在属性进入新对象时更新值,但我当然会必须添加与对象中的属性一样多的条件。

Meaning I will only update the value if the property comes in the new object, but of course I would have to add as many conditions as there are properties in the object.

这种方法很好,但它没有推广,所以如果对象有10个属性,我将不得不写10个条件。

This approach is fine, but it's not generalised so if the object has 10 properties, I would have to write 10 conditions.

有没有办法编写一个可以有条件地更新属性的函数,这样我就不必编写10个(或更多)条件了?

Is there a way of writing a function that could conditionally update the properties so that I don't have to write the 10 (or more) conditions?

推荐答案

在评论中你说过:

这意味着我们必须保留展开语法和 Object.assign ,因为它们都会复制所有属性从 newObj oldObj

That means we have to leave spread syntax and Object.assign out of the picture, as both of them would copy all properties from newObj over to oldObj.

相反,我们可以使用一个简单的循环(如果出现这种情况,你可以创建一个函数,也许 updateObject ):

Instead, we can use a simple loop (and if this comes up a lot, you can create a function, perhaps updateObject):

for (const key of Object.keys(newObj)) {
  if (key in oldObj) {
    oldObj[key] = newObj[key];
  }
}
let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };
let newObj = {test2: 'newValue2', xyz: "don't copy me"};

for (const key of Object.keys(newObj)) {
  if (key in oldObj) {
    oldObj[key] = newObj[key];
  }
}
console.log(oldObj);

(您可能更喜欢 oldObj.hasOwnProperty(key)而不是oldObj 中的键,具体取决于您的更新规则。)

(You may prefer oldObj.hasOwnProperty(key) instead of key in oldObj, depending on your rules for updating.)

或使用相对新的 Object.entries 和一些解构:

Or using the relatively-new Object.entries and some destructuring:

for (const [key, value] of Object.entries(newObj)) {
  if (key in oldObj) {
    oldObj[key] = value;
  }
}
let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };
let newObj = {test2: 'newValue2', xyz: "don't copy me"};

for (const [key, value] of Object.entries(newObj)) {
  if (key in oldObj) {
    oldObj[key] = value;
  }
}
console.log(oldObj);

在上面提到的澄清之前,我发布了以下有​​关差价和 Object.assign 的信息。仅为了完整性,但它们不适用于您想要跳过 oldObj 没有的属性的情况:

Before the clarification quoted above, I'd posted the following about spread and Object.assign. Just for completeness, but they don't apply to your case where you want to skip properties that oldObj doesn't have:

你不必像传播语法一样复杂,只需:

You don't have to be anything like that complicated with the spread syntax, just:

oldObj = {...oldObj, ...newObj};
let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };
let newObj = {test2: 'newValue2', xyz: "I get copied too"};

oldObj = {...oldObj, ...newObj};
console.log(oldObj);

这将创建一个包含所有 oldObj newObj 的属性的新对象, newObj 如果两者的名称相同,则获胜。

That will create a new object with all of oldObj's and newObj's properties, with newObj's properties winning if both have the same name.

请注意,物业价差为品牌 - 新的,刚被批准进入Stage  4(将在ES2018规范中)。如果您不想使用Stage  4提案,请使用 Object.assign

Note that property spread is brand-new, just approved to Stage 4 (will be in the ES2018 spec). If you don't want to use a Stage 4 proposal, use Object.assign:

oldObj = Object.assign({}, oldObj, newObj);
let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };
let newObj = {test2: 'newValue2', xyz: "I get copied too"};

oldObj = Object.assign({}, oldObj, newObj);
console.log(oldObj);

如果你想在原地更新 oldObj ,你也可以使用 Object.assign 创建一个新对象:

You'd also use Object.assign if you want to update oldObj in-place instead of creating a new object:

Object.assign(oldObj, newObj);
let oldObj = {test1: 'valueTest1',test2: 'valueTest2',test3: 'valueTest3' };
let newObj = {test2: 'newValue2', xyz: "I get copied too"};

Object.assign(oldObj, newObj);
console.log(oldObj);

这篇关于在Javascript中,如何有条件地更新对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:03