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

时间:2017-06-04 11:59:32

标签: javascript vue.js vuejs2

我试图在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

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

1 个答案:

答案 0 :(得分:3)

可能的解决方案:通过您的数据直接map。此代码创建一个数组,其中每个元素都是一个对象,表示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)