lodash.js select like mysqls`in`

问题描述 投票:0回答:1

我正在使用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)"。我该怎么办?

javascript json lodash
1个回答
0
投票

您可以在集合上使用破折号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>
© www.soinside.com 2019 - 2024. All rights reserved.