2016-05-31 69 views
0

我得到的字符串使用lodash

var users = ["[email protected]","[email protected]"]; 

的陣列比較來自串並jsons數組的數組信息我jsons也得到了數組:

var usersData = [ 
{ 
"email" : "[email protected]", 
"pass" : "1234", 
"age" : 19 
}, 
{ "email" : "[email protected]", 
"pass" : "123", 
"age" : 20 
}, 
{ "email" : "[email protected]", 
"pass" : "1234", 
"age" : 25 
}, 
{ "email" : "[email protected]", 
"pass" : "1234", 
"age" : 28 
}, 
{ "email" : "[email protected]", 
"pass" : "13go", 
"age" : 35 
}]; 

我想比較JSON和之間的電子郵件數組,json應該停留 - >使用lodash。

輸出 - JSON的,只有用戶陣列中

+0

什麼是預期輸出? –

+0

只輸出數組中的用戶Jsons數組 – MusicGindos

回答

1

你可以使用filter(),並使用flow()組成一個回調,partial()partialRight()

_.filter(usersData, _.flow(
    _.identity, 
    _.partialRight(_.get, 'email'), 
    _.partial(_.includes, users) 
)); 

這基本上等同於:

_.filter(usersData, item => _.includes(users, item.email)); 

在這種情況下,單線可能會更好,但它是方便的,能夠c也設置回調!

+0

hi adam,working,ty! – MusicGindos