简体   繁体   中英

lodash.js select like mysqls `in`

I'm using lodash.js to filter my json value. I need something like Mysqls "in" function in Lodash.

json value:
[
{id:1,customer:5},{id:2,customer:6},{id:3,customer:6},{id:2,customer:7}
]

I want to select "customer in(6,7)" in lodash. How can I do it?

You can use lodashes filter function on collections.

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM