gpt4 book ai didi

javascript - 如何在 Javascript 或 VueJs 中格式化数据对象

转载 作者:行者123 更新时间:2023-11-28 17:56:22 26 4
gpt4 key购买 nike

我正在尝试在 VueJs 中构建小型应用程序,其中我接收以下格式的数据:

{
"interactions":[
{
"id":14,
"user_id":1,
"schedule":"2017-06-04 05:02:12",
"type":"Meeting",
"with_client":0,
"event_type":"2",
"venue":"Mumbai",
"created_at":"2017-06-04 07:15:37",
"updated_at":"2017-06-04 07:15:37",
"deleted_at":null,
"meeting":{
"id":14,
"user_id":1,
"schedule":"2017-06-04 05:02:12",
"type":"Meeting",
"with_client":0,
"event_type":"2",
"venue":"Mumbai",
"created_at":"2017-06-04 07:15:37",
"updated_at":"2017-06-04 07:15:37",
"deleted_at":null,
"clients_association":[
{
"id":4,
"company_id":8,
"salutation":"Mr",
"first_name":"Check 2",
"last_name":"Contact",
"number":"098765",
"email":"check2@contact.com",
"alt_email":null,
"address":null,
"city":null,
"state":null,
"country":null,
"profile":"Investor-Senior",
"sectors_interested":"[\"Financial Services\",\"Metals & Mining\",\"Real Estate\",\"Cement\"]",
"companies_interested":"[9]",
"created_at":"2017-06-03 06:29:38",
"updated_at":"2017-06-03 06:29:38",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":4
}
},
{
"id":5,
"company_id":9,
"salutation":"Ms",
"first_name":"Ammy",
"last_name":"Contact",
"number":null,
"email":"ammy@contact.com",
"alt_email":null,
"address":null,
"city":null,
"state":null,
"country":null,
"profile":"Company-Promoter",
"sectors_interested":"[\"Pharmaceuticals\",\"Infrastructure\",\"Metals & Mining\",\"Auto\",\"Auto Ancillaries\",\"Real Estate\",\"Telecoms\",\"Capital Goods\"]",
"companies_interested":"[7]",
"created_at":"2017-06-03 06:30:50",
"updated_at":"2017-06-03 06:30:50",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":5
}
}
],
"contacts_association":[
{
"id":2,
"company_id":5,
"salutation":"Mr",
"first_name":"Check",
"last_name":"Contact",
"number":"234567890",
"email":"check@contact.com",
"alt_email":null,
"address":"Thane",
"city":"Thane",
"state":"Maharastra",
"country":"India",
"profile":"Research-Corporate Access",
"sectors_interested":"[\"Infrastructure\",\"Financial Services\",\"Capital Goods\",\"Pharmaceuticals\",\"Real Estate\"]",
"companies_interested":"[7]",
"created_at":"2017-06-02 19:32:30",
"updated_at":"2017-06-02 19:32:30",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":2
}
},
{
"id":3,
"company_id":4,
"salutation":"Mr",
"first_name":"Check 1",
"last_name":"Contact",
"number":null,
"email":"check1@contact.com",
"alt_email":null,
"address":null,
"city":null,
"state":null,
"country":null,
"profile":"Investor-Research Head",
"sectors_interested":"[\"Economics\",\"Real Estate\",\"Auto\",\"Consumer\",\"Logistics\",\"Oil & Gas\",\"Industrial\",\"Capital Goods\"]",
"companies_interested":"[8]",
"created_at":"2017-06-03 06:28:03",
"updated_at":"2017-06-03 06:28:03",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":3
}
},

],
"stellar_participants":[
{
"id":1,
"name":"Analyst",
"email":"analyst@example.com",
"address":null,
"city":null,
"state":null,
"country":null,
"role":"Analyst",
"supervisor_id":null,
"created_at":null,
"updated_at":null,
"deleted_at":null,
"pivot":{
"interaction_id":14,
"user_id":1
}
}
]
}
},
]
}

正在收到更多信息,我希望采用如下格式:

forEach(client_association)
{
if(this.client_name == null)
{
this.client_name = client_association.first_name+ ' '+client.assciation.last_name
}
else
{
this.client_name = this.client_name + ', ' + client_association.first_name+ ' '+client_association.last_name
}
}
forEach(stellar_participants)
{
if(this.stellar_participants_name == null)
{
this.stellar_participants_name = stellar_participants.name
}
else
{
this.stellar_participants_name = this.stellar_participants_name + ', ' + stellar_participants.name
}
}
forEach(contacts_participants)
{
if(this.contacts_participants_name == null)
{
this.contacts_participants_name = contacts_participants.first_name + ' ' + contacts_participants.last_name
}
else
{
this.contacts_participants_name = contacts_participants.first_name + ' ' + contacts_participants.last_name
}
}

所以我的最终格式将是:

meeting_date = 2017-06-04 05:02:12 //Schedule
meeting_call = Meeting //type
event_type = 2 //event_type
venue = Mumbai //venue
with_client = 0
stellar_participants = Analyst //stellarParticipants
clients_association = Check 2 Contact, Ammy Contact //adding all the names in clients association
contacts_association = Check Contact, Check 1 Contact //adding all the names in contacts association

这样它将它放入一个变量中,并且在过滤数据时变得更容易。请指导我如何实现这一目标。谢谢

最佳答案

可能的解决方案:只需简单地映射您的数据即可。此代码创建一个数组,其中每个元素都是代表 interactions 数组中一个元素的对象:

var a = {
"interactions":[
{
"id":14,
"user_id":1,
"schedule":"2017-06-04 05:02:12",
"type":"Meeting",
"with_client":0,
"event_type":"2",
"venue":"Mumbai",
"created_at":"2017-06-04 07:15:37",
"updated_at":"2017-06-04 07:15:37",
"deleted_at":null,
"meeting":{
"id":14,
"user_id":1,
"schedule":"2017-06-04 05:02:12",
"type":"Meeting",
"with_client":0,
"event_type":"2",
"venue":"Mumbai",
"created_at":"2017-06-04 07:15:37",
"updated_at":"2017-06-04 07:15:37",
"deleted_at":null,
"clients_association":[
{
"id":4,
"company_id":8,
"salutation":"Mr",
"first_name":"Check 2",
"last_name":"Contact",
"number":"098765",
"email":"check2@contact.com",
"alt_email":null,
"address":null,
"city":null,
"state":null,
"country":null,
"profile":"Investor-Senior",
"sectors_interested":"[\"Financial Services\",\"Metals & Mining\",\"Real Estate\",\"Cement\"]",
"companies_interested":"[9]",
"created_at":"2017-06-03 06:29:38",
"updated_at":"2017-06-03 06:29:38",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":4
}
},
{
"id":5,
"company_id":9,
"salutation":"Ms",
"first_name":"Ammy",
"last_name":"Contact",
"number":null,
"email":"ammy@contact.com",
"alt_email":null,
"address":null,
"city":null,
"state":null,
"country":null,
"profile":"Company-Promoter",
"sectors_interested":"[\"Pharmaceuticals\",\"Infrastructure\",\"Metals & Mining\",\"Auto\",\"Auto Ancillaries\",\"Real Estate\",\"Telecoms\",\"Capital Goods\"]",
"companies_interested":"[7]",
"created_at":"2017-06-03 06:30:50",
"updated_at":"2017-06-03 06:30:50",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":5
}
}
],
"contacts_association":[
{
"id":2,
"company_id":5,
"salutation":"Mr",
"first_name":"Check",
"last_name":"Contact",
"number":"234567890",
"email":"check@contact.com",
"alt_email":null,
"address":"Thane",
"city":"Thane",
"state":"Maharastra",
"country":"India",
"profile":"Research-Corporate Access",
"sectors_interested":"[\"Infrastructure\",\"Financial Services\",\"Capital Goods\",\"Pharmaceuticals\",\"Real Estate\"]",
"companies_interested":"[7]",
"created_at":"2017-06-02 19:32:30",
"updated_at":"2017-06-02 19:32:30",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":2
}
},
{
"id":3,
"company_id":4,
"salutation":"Mr",
"first_name":"Check 1",
"last_name":"Contact",
"number":null,
"email":"check1@contact.com",
"alt_email":null,
"address":null,
"city":null,
"state":null,
"country":null,
"profile":"Investor-Research Head",
"sectors_interested":"[\"Economics\",\"Real Estate\",\"Auto\",\"Consumer\",\"Logistics\",\"Oil & Gas\",\"Industrial\",\"Capital Goods\"]",
"companies_interested":"[8]",
"created_at":"2017-06-03 06:28:03",
"updated_at":"2017-06-03 06:28:03",
"deleted_at":null,
"pivot":{
"interaction_id":14,
"contact_id":3
}
},

],
"stellar_participants":[
{
"id":1,
"name":"Analyst",
"email":"analyst@example.com",
"address":null,
"city":null,
"state":null,
"country":null,
"role":"Analyst",
"supervisor_id":null,
"created_at":null,
"updated_at":null,
"deleted_at":null,
"pivot":{
"interaction_id":14,
"user_id":1
}
}
]
}
},
]
};

var res = a.interactions.map(i => Object.assign({
'meeting_date': i.schedule,
'meeting_call': i.type,
'event_type': i.event_type,
'venue': i.venue,
'with_client': i.with_client
}, {
'stellar_participants': i.meeting.stellar_participants.map(sp => sp.name).join(', ')
}, {
'clients_association': i.meeting.clients_association.map(ca => ca.first_name + ' ' + ca.last_name).join(', ')
}, {
'contacts_association': i.meeting.contacts_association.map(ca => ca.first_name + ' ' + ca.last_name).join(', ')
}));

console.log(res)

关于javascript - 如何在 Javascript 或 VueJs 中格式化数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com