A feature in Lodash called 'exclude' using both property and array input

I need to create a function that uses lodash's "without" method, which involves an object property and an array of values, similar to the "include" method.

Here is my code for the "_.include()" function:

 this.filters = {} //function for filtering data in table based on column name
 this.columnName = 'id'

 this.tempQuery = [1,2]

 this.data = [
 {
  id: 1,
  name: 'aaa',
  rejectedNumber: 1
 },  
 {
  id: 2,
  name: 'bbb',
  rejectedNumber: 2
 },
 {
  id: 3,
  name: 'ccc',
  rejectedNumber: 3
 }
]

 this.filters[this.columnName] = _.partial(_.includes, this.tempQuery);

 this.filteredData = _.filter( this.data, _.conforms( this.filters ));

And here is the resulting output:

 this.filteredData = [ 
{
  id: 1,
  name: 'aaa',
  rejectedNumber: 1
 },  
 {
  id: 2,
  name: 'bbb',
  rejectedNumber: 2
 }]

However, I am unable to achieve the same results with the "_.without" function.

this.filters[this.columnName] = _.partial(_.without, this.tempQuery);

Unfortunately, it did not work as expected and returned all the data...

Does anyone have any suggestions on how to implement this using lodash functions?

Answer №1

Replacing _.includes with _.without is not a simple task, as they are not directly opposites of each other. While both functions can take an array as their first argument, their purposes and outputs differ:

  • _.includes returns a boolean indicating whether the second argument exists in the array

  • _.without returns a new array that excludes the specified value(s) from the original array.

Since arrays are always considered truthy, using _.conform based on _.without will always yield a true result.

To create the true opposite of _.includes, you can utilize _.negate instead of _.without:

this.filters[this.columnName] = _.partial(_.negate(_.includes), this.tempQuery);

If you prefer not to modify your use of _.filter, another option could be to negate it using _.reject.

Answer №2

To filter your collection, you can use the _.filter method to iterate over it and return a truthy or falsey value based on your filtering criteria. Another option is to check if a specific value exists in the collection using the _.includes method. By combining these methods, you can create a new array with the filtered data.

Check out this Pen for a working example: https://codepen.io/joshuakelly/pen/aPgLmL

const tempQuery = [1, 2];
const data = [
 {
  id: 1,
  name: 'aaa',
  rejectedNumber: 1
 },  
 {
  id: 2,
  name: 'bbb',
  rejectedNumber: 2
 },
 {
  id: 3,
  name: 'bbb',
  rejectedNumber: 3
 }
];

const withFilteredData = _.filter(data, col => _.includes(tempQuery, col.id));
const withoutFilteredData = _.filter(data, col => !_.includes(tempQuery, col.id));

console.log(withFilteredData, withoutFilteredData);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a Typescript error while attempting to convert a JavaScript file to Typescript within an Express

After deciding to transition my express.js code to typescript, I have encountered an issue with my user model. Below is a simplified version of the code: //user.model.ts import { Schema, Types } from 'mongoose'; export interface User { na ...

A guide on utilizing the React Suite range slider in rsuite

Hello there, I recently started working with the UI framework rsuite (React suite) and everything was going smoothly until I tried to use the Range slider component's API. Unfortunately, I am facing some issues with the labels and tooltips not display ...

Using ReactJS and Ruby on Rails to implement an AJAX delete request

There is a component named Items residing inside a parent component called ItemsContainer. A click on a button within the Items component triggers an Ajax function to delete that specific Item. However, I am encountering a 500 error message at the moment ...

Ways to invoke a slice reducer from a library rather than a React component

I've been working on a React application using the React Boilerplate CRA Template as my foundation. This boilerplate utilizes Redux with slices, which is great for updating state within a React component. However, I'm facing a challenge when try ...

Ways to transfer a variable from a Node.js script to a JavaScript file on the client side

Seeking guidance on how to transfer the "myBlogs" variable from my Node.js file ("server.js") to a frontend JavaScript file ("blogs.js"). I intend to utilize this variable in the JS file to iterate through the array it contains, generating a template for e ...

Display the directive after including JavaScript on the webpage

My website includes an angular application, angular directive, controller, and various javascript functions. One of these functions involves dynamically adding HTML code to the page, which includes one of my angular directives. However, I've noticed t ...

Create a single HTML page with multiple images displayed

I've come up with a clever solution to display images in one HTML file and show them full-sized in another. Instead of creating multiple HTML files, I want to achieve this with just one. By using JavaScript, when a user clicks on an image in the first ...

Class fully loaded with Angular 2

I am facing a challenge where I have 4 components that need to receive a class "loaded" when they are ready. However, I am uncertain about the best approach to handle this situation effectively. These components are not contained in an ngFor loop, so they ...

Including JavaScript in HTML Error 404

https://i.stack.imgur.com/aQDPG.png I am struggling to understand why this import is not functioning as expected. I have tried using script/import.js but it still fails to work. The error message I keep receiving is: 127.0.0.1 - - [09/Sep/2020 15:09:35] ...

Trilateration of three-dimensional points using K vertices on each face

I'm currently working on a project using Three.js and I have a set of 3D points in the form of (x, y, z) coordinates, as well as a series of faces. Each face consists of K points and can be either convex or concave in shape. Despite consulting the doc ...

TreeView Filtering

I have encountered an issue while trying to utilize a treeview filter. Below is the method I have created: var tree = [{ id: "Tree", text: "Tree", children: [ { id: "Leaf_1", text: "Leaf 1", childre ...

Switch out the URL in npm's build process

While developing, I am using a mock REST service but for the public release, I intend to switch to a real backend. Is there a method to update the endpoint URL during the npm build process? ...

There seems to be an issue with the next function's functionality within a Nodejs middleware

Currently, I am delving into the world of Nodejs with expressjs. My focus is on understanding middleware functions and specifically, the role of "next". In the middleware concept, "next" simply moves on to the next middleware in line. So, what exactly is ...

Dynamic backdrop featuring engaging content

I am currently working on a WordPress website that features a dynamic background. While I have successfully implemented movement to the background, the content on the site remains static and unaffected by scrolling. The background does not move alongside t ...

Having trouble getting the ng-bootstrap Rating component to function on Angular 6?

I'm attempting to utilize Angular 6, ng-bootstrap, and the Rating feature, but I'm encountering issues. Does anyone have a solution for properly implementing this rating functionality? NG-RATE Here is my code: facilitistatus.component.html &l ...

Floating motion when dragging

I am looking to create a hover effect while dragging an object over other objects. The catch is that the hoverable objects are inside an iframe, while the draggable object is outside the iframe. You can see what I am trying to achieve in this example ...

Transferring information to the specified link using AJAX and PHP

I'm having trouble sending data to a link that I click on. Whenever I try, I just end up with an undefined index error in my PHP file. <a href="output.php"> Click me </a> ('a').click(function(){ href = "output.php"; ...

My software consistently triggers the default servlet

I'm encountering an issue with calling a servlet and I could use some assistance. Here is a snippet from my web.xml file: <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.s ...

Angular-Daterangepicker: Dynamically Updating Start Date Selection

I am currently utilizing the date-range picker from https://github.com/fragaria/angular-daterangepicker and to showcase a calendar for selecting travel dates in my application. Depending on the type of flight chosen, I need either a date range for roundtr ...

Is there a way to use Javascript to determine if a string within a JSON object has been altered?

I am looking for a way to continuously monitor changes in a specific string or date stored in a JSON file. How can I effectively store this value and create a mechanism to compare it for any differences? Any assistance would be highly appreciated. // Ex ...