使用 lodash 比较来自字符串数组和 json 数组的信息

compare info from array of strings and array of jsons using lodash

我得到了字符串数组

var users = ["user1@gmail.com","user2@gmail.com"];

我也得到了 jsons 的数组:

var usersData = [
{
"email" : "user1@gmail.com",
"pass" : "1234",
"age" : 19
},
{ "email" : "user2@gmail.com",
"pass" : "123",
"age" : 20
},
{ "email" : "user3@gmail.com",
"pass" : "1234",
"age" : 25
},
{ "email" : "user4@gmail.com",
"pass" : "1234",
"age" : 28
},
{ "email" : "user5@gmail.com",
"pass" : "13go",
"age" : 35
}];

我想比较 json 和数组之间的电子邮件,json 应该保留 -> 使用 lodash。

输出 - Json 仅包含数组

中的用户

您可以使用 filter() and compose a callback using flow(), partial(), and partialRight():

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

本质上等同于:

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

在这种情况下,单行代码可能更好,但能够编写回调也很方便!