Armagetin Armagetin - 13 days ago 4x
Javascript Question

How do you do the IN (SQL) operation using underscore js?

I am trying to query json using underscore js and im kinda stuck. I want to do an IN operation like from SQL but im not sure how to do it using the _.where function.

For example,

In SQL the query would be like:

where n = 10 AND color in ('red', 'pink', 'black');

But when writing this in underscore I can't do this (recordset is an array of objects)

_.where( recordset, { n: 10, color: 'red' || 'pink' || 'black'});

That just gives me objects where n = 10 and color is only red. Any other way to do this?

Thanks in advance

let colors = ['red', 'pink', 'black']
let resultset =, e => e.n === 10 && _.contains(colors, e.color))

or in older js

var resultset =, function (e) {
    return e.n === 10 && _.contains(['red', 'pink', 'black'], e.color);
}); takes an array as the first argument (equivalent to the FROM in SQL) and a function returning true/false as the second argument (equivalent to the WHERE in SQL).