I would like to make a function using lodash's "without", using object property and array of values. Similar to "include"
My "_.include()" function code is:
this.filters = {} //hold filter function for one or many columns in table
this.columnName = 'id'
this.tempQuery = [1,2]
this.data = [
{
id: 1,
name: 'aaa',
rejectedNumber: 1
},
{
id: 2,
name: 'bbb',
rejectedNumber: 2
},
{
id: 3,
name: 'bbb',
rejectedNumber: 3
}
]
this.filters[this.columnName] = _.partial(_.includes, this.tempQuery);
this.filteredData = _.filter( this.data, _.conforms( this.filters ));
And my output is
this.filteredData = [
{
id: 1,
name: 'aaa',
rejectedNumber: 1
},
{
id: 2,
name: 'bbb',
rejectedNumber: 2
}]
But i can't do something similar with "_.without" function
this.filters[this.columnName] = _.partial(_.without, this.tempQuery);
Not working - return all data...
Any ideas how to make query using lodash functions ?
The reason you cannot just replace _.includes
with _.without
is that they are not each other's opposite. They have in common that the first argument they take can be an array (which is what you use for it). But there the similarity ends; they are quite different:
_.includes
returns a boolean indicating whether the second argument occurs in the array
_.without
returns a array that includes the values in the original array except the second argument (and other argument values if provided).
As an array is always a truthy value, the _.conform
function -- when based on _.without
-- will always return true.
Instead of _.without
, create the real opposite of _.includes
with _.negate
:
this.filters[this.columnName] = _.partial(_.negate(_.includes), this.tempQuery);
I suppose that you do not want to touch the use of _.filter
, but just as an alternative, you could also negate that with _.reject
.