我正在使用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)"
。我该怎么办?
您可以在集合上使用破折号filter函数。另外,您的JSON数据似乎有些偏离。我已经在以下示例中修复了它。
const input = '{"value": [{"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.value, 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>