我正在Vue Js 2.0
构建一个小应用程序,我正在尝试过滤数组格式的元素,如果元素是单字符串,我要格式化:
const search = this.search_by_name.toLowerCase()
const searchContact = this.search_by_contact.toLowerCase()
return this.meetings
.map(i => ({
id: i.interaction.id,
client_name: i.client.name,
contactName: i.contacts_association,
}))
.filter((item) =>
item.client_name.toLowerCase().includes(search)
)
我希望item.contactName.first_name.toLowerCase().includes(searchContact)
但在这种情况下,contacts_association
采用数组格式,会返回错误:
TypeError:item.contactName.toLowerCase不是函数
我的contacts_association
采用以下格式:
"contacts_association":[
{
"id":431,
"first_name":"Nikunj",
"last_name":"Doshi",
"address":"602-603, 6th Floor, Dalamal House,Nariman Point",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"pivot":
{
"interaction_id":139,
"contact_id":431,
"company_id":160,
"created_at":"2017-09-23 16:42:56",
"updated_at":"2017-09-23 16:42:56"
},
"company":[
{
"id":160,
"name":"Bay Capital Investment Managers Pvt Ltd",
"address":"602-603, 6th Floor, Dalamal House,Nariman Point",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"type":"Investor",
"sub_type":"FII",
"is_client":0,
}
]
}
{
"id":431,
"first_name":"Vikash",
"last_name":"Kumar",
"address":"602-603, 6th Floor, Dalamal House,Nariman Point",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"pivot":
{
"interaction_id":139,
"contact_id":431,
"company_id":160,
"created_at":"2017-09-23 16:42:56",
"updated_at":"2017-09-23 16:42:56"
},
"company":[
{
"id":160,
"name":"Investment Managers Pvt Ltd",
"address":"Nariman Point",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"type":"Investor",
"sub_type":"FII",
"is_client":0,
}
]
}
]
我想要使用first_name
和last_name
过滤器,请帮助我。
答案 0 :(得分:4)
所以基本上你说contactName
是一系列联系人,而不只是一个,你希望匹配searchContact
中至少有一个名称匹配的联系人的所有会议{ {1}}?
试试这个(未经测试):
.filter(item =>
item.client_name.toLowerCase().includes(search) &&
item.contactName.some(c =>
(c.first_name + ' ' + c.last_name).toLowerCase().includes(searchContact)
)
)
另外我应该提一下,分别为contactName
和client_name
混合camelCase
和snake_case
会让人感到困惑。选一个。