我将JSON object
格式化为
{
id: 'a60c09fa-9b60-4b14-9ee6-41d64d243435',
contactData: {
phone: '526 669 77',
email: 'client@test.ee',
skype: 'skype',
company_name: 'CLIent22',
address: 'address',
contact_person: 'Person'
}
}
我想将其转换为
{
id: 'a60c09fa-9b60-4b14-9ee6-41d64d243435',
phone: '526 669 77',
email: 'client@test.ee',
skype: 'skype',
company_name: 'CLIent22',
address: 'address',
contact_person: 'Person'
}
如何做到这一点。寻找你的帮助和建议。
答案 0 :(得分:3)
:
let result = {
id: input.id,
...input.contactData
}
或使用jQuery:
var result = $.extend({ id: input.id }, input.contactData);
答案 1 :(得分:1)
怎么样?
var yourobject ={
id: 'a60c09fa-9b60-4b14-9ee6-41d64d243435',
contactData: {
phone: '526 669 77',
email: 'client@test.ee',
skype: 'skype',
company_name: 'CLIent22',
address: 'address',
contact_person: 'Person'
}
}
var newObject=yourobject.contactData;
newObject.id=yourobject.id;
答案 2 :(得分:0)
您可以使用Object.create
生成对象的副本,而不会改变原始对象。然后添加其他属性。
var original = { id: 'a60c09fa-9b60-4b14-9ee6-41d64d243435', contactData: { phone: '526 669 77', email: 'client@test.ee', skype: 'skype', company_name: 'CLIent22', address: 'address', contact_person: 'Person' } },
result = Object.create(original.contactData);
result.id = original.id;
console.log(result);
console.log(original);
.as-console-wrapper { max-height: 100% !important; top: 0; }