2017-06-04 59 views
0

我試圖建立VueJs小應用程序,其中我在下面的格式接收數據:到如何在Javascript格式的數據對象或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":"[email protected]", 
         "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":"[email protected]", 
         "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":"[email protected]", 
         "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":"[email protected]", 
         "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":"[email protected]", 
         "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 

,這樣它將它變成一個變量,並且在過濾數據時變得更容易。請指導我如何實現這一目標。謝謝

+0

目前還不清楚你所需要的:每個元素代表interactions一個數組元素的對象時,此代碼創建一個陣列。您可以根據收到的數據顯示所需的最終數據對象而不是代碼?看起來它與Vue無關,並且與JS數據過濾有更多共同之處。 – wostex

+0

@wostex我已更新該問題,請查看。 –

+0

你想在一個對象中有哪些部分? –

回答

3

可能的解決方案:只需簡單地通過您的數據map

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":"[email protected]", 
 
         "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":"[email protected]", 
 
         "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":"[email protected]", 
 
         "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":"[email protected]", 
 
         "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":"[email protected]", 
 
         "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 
 
    }, { 
 
    \t 'stellar_participants': i.meeting.stellar_participants.map(sp => sp.name).join(', ') 
 
    }, { 
 
    \t 'clients_association': i.meeting.clients_association.map(ca => ca.first_name + ' ' + ca.last_name).join(', ') 
 
    }, { 
 
    \t 'contacts_association': i.meeting.contacts_association.map(ca => ca.first_name + ' ' + ca.last_name).join(', ') 
 
    })); 
 

 
console.log(res)

相關問題