javascript - lodash.js 选择像 mysqls `in`
问题描述
我正在使用 lodash.js 过滤我的 json 值。我在 Lodash 中需要类似 Mysqls “in”函数的东西。
json value:
[
{id:1,customer:5},{id:2,customer:6},{id:3,customer:6},{id:2,customer:7}
]
我想"customer in(6,7)"
在 lodash 中选择。我该怎么做?
解决方案
您可以在集合上使用 lodashes过滤器功能。
const input = '[{"id":1,"customer":5},{"id":2,"customer":6},{"id":3,"customer":6},{"id":2,"customer":7}]';
const mysqlIn = (json, array) => {
const data = JSON.parse(json);
let ans = _.filter(data, function(x) {
return array.includes(x.customer);
});
return ans;
}
console.log(mysqlIn(input, [6,7]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
推荐阅读
- node.js - 如何在路由器文件中使用负载视图引擎?
- javascript - 我想从 Firebase 实时数据库中获取数据并希望在我的 HTML 页面中显示
- sql - 检查 ROW 中的 EMAIL 格式并在 SELECT 查询中返回值
- python-2.7 - 在 CentOS7 上安装时出现 AirFlow 错误
- flutter - 如何解决 pubspec.yaml 中的映射值错误?
- mongodb - MongoDB查询计划不使用复合索引
- asp.net-mvc - 从发布版本中排除脚手架包是否安全?
- javascript - 防止 Angular Web 组件监听 shadow DOM 之外的 keydown 事件
- python - 使用 pydub 访问 ffprobe
- java - 传递包含空格的争论的Java运行时问题?