如何使用lodash过滤对象?

时间:2018-12-11 18:30:22

标签: reactjs lodash

我有任何对象数组,如下所示。并且我的代理人/客户sid名称为“ agentCallSid”,例如CAb56eb286a9431e29c776a989747af817。我想过滤参与者包含agentCallSid的对象。参与者也是一个字符串,如下面的数组所示。

[{
        conferenceSid: 'CF0339437ea7f4511af474dcaa1d9eb822',
        conference_name: '3534',
        conference_status: 'in-progress',
        user_id: 100112,
        date_time: '2018-12-11T10:39:55.000Z',
        participants: '[{"role": "agent", "username": "aamer", "agentCallSid": "CAb30187cd4350fbaac6509bb333e0058a", "inCallStatus": true, "callStartTime": "2018-12-11T10:38:35.243Z"}, {"role": "client", "username": "shahzad", "inCallStatus": true, "callStartTime": "2018-12-11T10:38:35.243Z", "clientCallSid": "CA12b3797dffa2f8c210e6e6bc5c1560f9"}]'
    },
    {
        conferenceSid: 'CF264305d1571df65562e38014bf968f1a',
        conference_name: '6618',
        conference_status: 'in-progress',
        user_id: 100112,
        date_time: '2018-12-11T10:40:50.000Z',
        participants: '[{"role": "agent", "username": "aamer", "agentCallSid": "CAb56eb286a9431e29c776a989747af817", "inCallStatus": true, "callStartTime": "12/11/2018"}, {"role": "client", "username": "shahzad", "inCallStatus": true, "callStartTime": "12/11/2018", "clientCallSid": "CAca658f5660167fca61d37d6fdd04f5eb"}]'
    },
    {
        conferenceSid: 'CFd3d0e940f0274bd3e8b939d40603e62b',
        conference_name: '893',
        conference_status: 'in-progress',
        user_id: 100116,
        date_time: '2018-12-11T11:50:44.000Z',
        participants: '[{"role": "agent", "callSid": "CA7ee924fe702041ac1be6396e394e59f4", "username": "aamer", "inCallStatus": true, "callStartTime": "2018-12-11T11:49:23.533Z"}, {"role": "client", "callSid": "CA5087bedf915c0f78e684f548d413393c", "username": "zain", "inCallStatus": true, "callStartTime": "2018-12-11T11:49:23.533Z"}]'
    }
]

1 个答案:

答案 0 :(得分:2)

您可以使用_.filter_.some这样的内容:

const data = [{ conferenceSid: 'CF0339437ea7f4511af474dcaa1d9eb822', conference_name: '3534', conference_status: 'in-progress', user_id: 100112, date_time: '2018-12-11T10:39:55.000Z', participants: '[{"role": "agent", "username": "aamer", "agentCallSid": "CAb30187cd4350fbaac6509bb333e0058a", "inCallStatus": true, "callStartTime": "2018-12-11T10:38:35.243Z"}, {"role": "client", "username": "shahzad", "inCallStatus": true, "callStartTime": "2018-12-11T10:38:35.243Z", "clientCallSid": "CA12b3797dffa2f8c210e6e6bc5c1560f9"}]' }, { conferenceSid: 'CF264305d1571df65562e38014bf968f1a', conference_name: '6618', conference_status: 'in-progress', user_id: 100112, date_time: '2018-12-11T10:40:50.000Z', participants: '[{"role": "agent", "username": "aamer", "agentCallSid": "CAb56eb286a9431e29c776a989747af817", "inCallStatus": true, "callStartTime": "12/11/2018"}, {"role": "client", "username": "shahzad", "inCallStatus": true, "callStartTime": "12/11/2018", "clientCallSid": "CAca658f5660167fca61d37d6fdd04f5eb"}]' }, { conferenceSid: 'CFd3d0e940f0274bd3e8b939d40603e62b', conference_name: '893', conference_status: 'in-progress', user_id: 100116, date_time: '2018-12-11T11:50:44.000Z', participants: '[{"role": "agent", "callSid": "CA7ee924fe702041ac1be6396e394e59f4", "username": "aamer", "inCallStatus": true, "callStartTime": "2018-12-11T11:49:23.533Z"}, {"role": "client", "callSid": "CA5087bedf915c0f78e684f548d413393c", "username": "zain", "inCallStatus": true, "callStartTime": "2018-12-11T11:49:23.533Z"}]' } ] 

const getAgentById = id => _.filter(data, x => {
  let arr = JSON.parse(x.participants)
  return _.some(arr, {agentCallSid: id})
})
console.log(getAgentById('CAb56eb286a9431e29c776a989747af817'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

使用Array.filterArray.some,不带破折号,您可以获得相同的结果:

const data = [{ conferenceSid: 'CF0339437ea7f4511af474dcaa1d9eb822', conference_name: '3534', conference_status: 'in-progress', user_id: 100112, date_time: '2018-12-11T10:39:55.000Z', participants: '[{"role": "agent", "username": "aamer", "agentCallSid": "CAb30187cd4350fbaac6509bb333e0058a", "inCallStatus": true, "callStartTime": "2018-12-11T10:38:35.243Z"}, {"role": "client", "username": "shahzad", "inCallStatus": true, "callStartTime": "2018-12-11T10:38:35.243Z", "clientCallSid": "CA12b3797dffa2f8c210e6e6bc5c1560f9"}]' }, { conferenceSid: 'CF264305d1571df65562e38014bf968f1a', conference_name: '6618', conference_status: 'in-progress', user_id: 100112, date_time: '2018-12-11T10:40:50.000Z', participants: '[{"role": "agent", "username": "aamer", "agentCallSid": "CAb56eb286a9431e29c776a989747af817", "inCallStatus": true, "callStartTime": "12/11/2018"}, {"role": "client", "username": "shahzad", "inCallStatus": true, "callStartTime": "12/11/2018", "clientCallSid": "CAca658f5660167fca61d37d6fdd04f5eb"}]' }, { conferenceSid: 'CFd3d0e940f0274bd3e8b939d40603e62b', conference_name: '893', conference_status: 'in-progress', user_id: 100116, date_time: '2018-12-11T11:50:44.000Z', participants: '[{"role": "agent", "callSid": "CA7ee924fe702041ac1be6396e394e59f4", "username": "aamer", "inCallStatus": true, "callStartTime": "2018-12-11T11:49:23.533Z"}, {"role": "client", "callSid": "CA5087bedf915c0f78e684f548d413393c", "username": "zain", "inCallStatus": true, "callStartTime": "2018-12-11T11:49:23.533Z"}]' } ] 

const getAgentById = id => data.filter(x => {
  let arr = JSON.parse(x.participants)
  return arr.some(y => y.agentCallSid === id)
})

console.log(getAgentById("CAb56eb286a9431e29c776a989747af817"))