我正在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_namelast_name过滤器,请帮帮我。

最佳答案

因此,基本上,您是说contactName是一组联系人而不是一个联系人,并且您想将名称与contactName匹配的所有在searchContact中具有至少一个联系人的会议进行匹配?

试试这个(未试用):

.filter(item =>
  item.client_name.toLowerCase().includes(search) &&
  item.contactName.some(c =>
    (c.first_name + ' ' + c.last_name).toLowerCase().includes(searchContact)
  )
)


我还要提到,分别为camelCasesnake_case混合contactNameclient_name令人困惑。选一个。

关于javascript - 如何从Vue JS中的数组元素过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47750984/

10-09 21:13