[英]How to merge multiple objects as separate objects
I am using Vue.js and I have 2 objects that I would like to combine together but to keep them separated.我正在使用 Vue.js 并且我有 2 个对象,我想将它们组合在一起,但要让它们分开。
I tried to use a spread such as var obj1 = {...obj2, ...obj3} but realized that this was overwriting values because names were the same.我尝试使用诸如 var obj1 = {...obj2, ...obj3} 之类的传播,但意识到这是覆盖值,因为名称相同。 I changed the billing address information to have "billing" in front of them and using that same process again, its just a very long list and not very clean to me.
我将帐单地址信息更改为在它们前面有“帐单”并再次使用相同的过程,它只是一个很长的列表,对我来说不是很干净。
Below are the 2 objects that I am trying to combine.以下是我要组合的 2 个对象。
deliveryAddress: {
firstName: null,
lastName: null,
companyName: null,
jobTitle: null,
address1: null,
address2: null,
city: null,
country: null,
province: null,
region: null,
state: null,
zipCode: null,
phoneNumber: null,
email: null
},
billingAddress: {
billingDifferentFromDelivery: false,
billingAddress1: null,
billingAddress2: null,
billingCity: null,
billingCountry: null,
billingState: null,
billingProvince: null,
billingZipCode: null,
}
I would like to combine these as either an object with both objects or as an array with both objects.我想将它们组合为具有两个对象的 object 或具有两个对象的数组。 Maybe something similar to this
也许与此类似
var addressInformation = {
deliveryAddress: {
firstName: null,
lastName: null,
companyName: null,
jobTitle: null,
address1: null,
address2: null,
city: null,
country: null,
province: null,
region: null,
state: null,
zipCode: null,
phoneNumber: null,
email: null
},
billingAddress: {
billingDifferentFromDelivery: false,
billingAddress1: null,
billingAddress2: null,
billingCity: null,
billingCountry: null,
billingState: null,
billingProvince: null,
billingZipCode: null,
}
}
or或者
var addressInformation = [{
deliveryAddress: {
firstName: null,
lastName: null,
companyName: null,
jobTitle: null,
address1: null,
address2: null,
city: null,
country: null,
province: null,
region: null,
state: null,
zipCode: null,
phoneNumber: null,
email: null
},
billingAddress: {
billingDifferentFromDelivery: false,
billingAddress1: null,
billingAddress2: null,
billingCity: null,
billingCountry: null,
billingState: null,
billingProvince: null,
billingZipCode: null,
}
}]
is this possible?这可能吗? is there a better way to clean this up when combining objects?
组合对象时有没有更好的方法来清理它?
You need to assign property name separately您需要单独分配属性名称
let addressInformation = {deliveryAddress : ...obj1, billingAddress: ...obj2}
or或者
let addressInformation = [{deliveryAddress : ...obj1}, {billingAddress: ...obj2}]
Or if you don't need a new copy then you can do something like this as well或者,如果您不需要新副本,那么您也可以这样做
let addressInformation = {deliveryAddress, billingAddress}
You could do it like this:你可以这样做:
let addressInformation = {
deliveryAddress,
billingAddress
}
with es6 there is no need for {key:value}
when both the key and the value variable have thesame name.对于 es6,当键和值变量具有相同名称时,不需要
{key:value}
。
I think you shouldn't spread your objs.我认为你不应该传播你的对象。
const obj1 = {name: 1}
const obj2 = {name: 2}
const newObj = {obj1, obj2}
// result to `{{name: 1}, {name: 2}}` on newObj
But I'm not sure about the question但我不确定这个问题
/.\ Keep in mind this not a copy object. /.\ 请记住,这不是一个复制对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.