简体   繁体   English

将房产从一个object移到另一个Javascript

[英]Move a property from one object to another in Javascript

The root of my problem is GraphQL does not allow fields that are not defined by my input type but the data object comes from a single form.我的问题的根源是 GraphQL 不允许我的输入类型未定义的字段,但数据 object 来自单个表单。

I would love for GraphQL to just ignore the extra field but from what I gathered, that the validation error is built by design.我希望 GraphQL 忽略额外的字段,但从我收集到的信息来看,验证错误是有意设计的。

So now, I need to move the property that GraphQL doesn't know and store it in another variable so I can use it in another mutation.所以现在,我需要移动 GraphQL 不知道的属性并将其存储在另一个变量中,以便我可以在另一个突变中使用它。

What I am doing:我在做什么:

const items = formData.items

delete formData.items

I am just wondering if there is any new/better syntax or technique for doing the same thing.我只是想知道是否有任何新的/更好的语法或技术来做同样的事情。

You can destructure from the original object and use spread to get two new variables, items and newFormData from your original formData :您可以从原始 object 解构并使用spread从原始formData获取两个新变量itemsnewFormData

const { items, ...newFormData } = formData

Doing a delete is a code smell.删除是一种代码味道。 It is better if you do:如果你这样做会更好:

 const formData = {myProp:8, items:[1,2,3], anotherProp:12} const {items:items, ...newFormData} = formData console.log(items) console.log(newFormData)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中将所选属性从一个对象复制到另一个对象 - How to copy selected property from one object to another in JavaScript 如果条件(JavaScript ES6),将一个特定的 object 道具从一个数组移动到另一个对象数组 - move one specific object prop from one array to another array of objects, if condition (JavaScript ES6) 将对象从一个画布移动到另一画布 - Move object from one canvas to another 将对象从一个数组移动到另一个数组 - Move object from one array to another JavaScript 检查一个 object 属性值是否存在于另一个 object 属性上 - JavaScript check if one object property value is exist on another object property 如何使用javascript将对象从一个列表移动到同一页面上的另一个列表 - How to use javascript to move an object from one list to another on the same page Javascript Object Literal从另一个属性引用另一个属性 - Javascript Object Literal referring to another property in itself from another property 从另一个JavaScript访问一个属性值 - access one property value from another javascript 将一个 object 中的属性添加为另一个 object 中的属性 - Add a property from one object, as a property in another object 如何从一个 object 中删除元素并将其移动到另一个? - How to remove element from one object and move it to another one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM