【问题标题】:How format data object in Javascript or VueJs如何在 Javascript 或 VueJs 中格式化数据对象
【发布时间】:2017-11-05 08:02:36
【问题描述】:

我正在尝试在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

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

【问题讨论】:

  • 不清楚您需要什么。您可以根据收到的数据显示所需的最终数据对象,而不是您的代码吗?貌似和 Vue 无关,和 JS 数据过滤有更多的共同点。
  • @wostex 我已经更新了问题,请查看。
  • 您希望在一个对象中包含哪些部分?
  • @NinaScholz 我正在获取数组元素,因此对于每个元素,我想获得信息 meeting_datemeeting_callevent_typevenuewith_client 现在也是 stellar_participants一个数组,所以我想将该数组与一个对象合并,该对象包含这些对象的所有名称,用逗号分隔,与clients_associationcontacts_association 相同,但客户和联系人有first_namelast_name

标签: javascript vue.js vuejs2


【解决方案1】:

可能的解决方案:通过您的数据直接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)

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 2018-10-26
    • 1970-01-01
    • 2017-11-16
    • 2019-03-20
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    相关资源
    最近更新 更多