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

时间:2019-03-27 16:41:00

标签: javascript object ecmascript-6

我有两个对象,一个用于更新另一个,类似于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'
      }
   ]
}

3 个答案:

答案 0 :(得分:1)

您可以使用forEachObject.entries

这里的想法是

  • 首先循环遍历myObject的{​​{1}}数组中的每个元素
  • 现在,就像在您的结构中一样,您在currentObject中将currentObject的值设为key,因此我们用updateObject来检查存在性
  • 如果是他们,我们会更新updateObject.myObject[value],否则我们将其保持不变

currentObject

答案 1 :(得分:1)

我们可以使用Array.reduce并在ele中搜索当前元素的(attribute3updateObject.myObject属性。

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

答案 2 :(得分:1)

这变成了具有最新JavaScript语言功能的单行代码:

class Ui_Card(object):
    def setupUi(self, Card):
        Card.setObjectName("Card")
        Card.setWindowModality(QtCore.Qt.NonModal)
        Card.setEnabled(True)
        Card.resize(771, 134)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, 
        QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(Card.sizePolicy().hasHeightForWidth())
        Card.setSizePolicy(sizePolicy)
        self.title = QtWidgets.QLabel(Card)
        self.title.setGeometry(QtCore.QRect(6, 10, 761, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.title.setFont(font)
        self.title.setObjectName("title")
        self.user = QtWidgets.QLabel(Card)
        self.user.setGeometry(QtCore.QRect(20, 40, 47, 13))
        font = QtGui.QFont()
        font.setPointSize(7)
        self.user.setFont(font)
        self.user.setObjectName("user")
        self.label_3 = QtWidgets.QLabel(Card)
        self.label_3.setGeometry(QtCore.QRect(70, 30, 20, 31))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.subreddit = QtWidgets.QLabel(Card)
        self.subreddit.setGeometry(QtCore.QRect(90, 40, 47, 13))
        font = QtGui.QFont()
        font.setPointSize(7)
        self.subreddit.setFont(font)
        self.subreddit.setObjectName("subreddit")
        self.horizontalLayoutWidget = QtWidgets.QWidget(Card)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 60, 771, 
        31))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = 
        QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, 
        QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.comments = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.comments.setObjectName("comments")
        self.horizontalLayout.addWidget(self.comments)
        self.upvote = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.upvote.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("upvoteArrow.jpg"), QtGui.QIcon.Normal, 
        QtGui.QIcon.Off)
        self.upvote.setIcon(icon)
        self.upvote.setFlat(True)
        self.upvote.setObjectName("upvote")
        self.horizontalLayout.addWidget(self.upvote)
        self.downvote = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.downvote.setFlat(True)
        self.downvote.setObjectName("downvote")
        self.horizontalLayout.addWidget(self.downvote)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, 
        QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.line = QtWidgets.QFrame(Card)
        self.line.setGeometry(QtCore.QRect(0, 100, 771, 21))
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setLineWidth(3)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setObjectName("line")

        self.retranslateUi(Card)
        QtCore.QMetaObject.connectSlotsByName(Card)

    def retranslateUi(self, Card):
        _translate = QtCore.QCoreApplication.translate
        Card.setWindowTitle(_translate("Card", "Form"))
        self.title.setText(_translate("Card", "TextLabel"))
        self.user.setText(_translate("Card", "TextLabel"))
        self.label_3.setText(_translate("Card", "-"))
        self.subreddit.setText(_translate("Card", "TextLabel"))
        self.comments.setText(_translate("Card", "TextLabel"))
        self.downvote.setText(_translate("Card", "downvote"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Card = QtWidgets.QWidget()
    ui = Ui_Card()
    ui.setupUi(Card)
    Card.show()
    sys.exit(app.exec_())

...您还将获得不变性。