0
我正在研究一个小API,我想尝试使用HTTP PATCH REQUEST
来更新数据,而不使用一堆if语句。我试图用变化的数据填充传出的数据对象。当属性不为空时将属性添加到对象
update() {
let prop1 = hasBeenChanged.prop1 ? changedData.prop1 : null;
// ...
let propN = hasBeenChanged.propN ? changedData.propN : null;
let data: IPatchProfile = {
// something like --> property != null ? property: property.value : nothing
}
}
有什么办法可以动态地创建数据对象吗?
谢谢。我现在明白了。我忘记说我正在使用打字稿,我认为它会很相似,但是当我编译到ES5时,在打字稿的ObjectConstructor中没有Object.assign功能。我用关于串化数据的小技巧做了它。当我将数据设置为undefined而不是null时,它们并未被串化,这非常简单!谢谢! –