Refine object array by applying multiple filters based on various values

Currently, I have an array of objects that I need to filter based on user input. For example, if a user enters "Red Shirt," I want to only return entries with values like

{color: "red", clothingType: "shirt"}

rather than

{color: "red", clothingType: "scarf"}
// or 
{color: "blue", clothingType: "shirt"}

I am hoping to create a generic solution that can be easily adapted for other scenarios, such as searching for "John Smith" in objects like

{firstName:"John", lastName:"Smith"},

Is there a straightforward way to implement this overall filter without requiring the user to specify color and clothing type separately, or is that impossible?

Currently, my approach involves retrieving the value of "data[key]" and using "includes" to check if "red" or "shirt" exist in the object individually. However, I am unsure how to search for multiple values simultaneously.

Answer №1

Here is a simple code snippet that may be helpful:

const input = "John K. Smith";
const data = [
  { firstName: "Carl", lastName: "Gauss" },
  { firstName: "John", lastName: "Smith" },
  { firstName: "Elon", lastName: "Musk" },
  { firstName: "John", lastName: "Kennedy" },
  { firstName: "Adam", lastName: "Smith" }
];

console.log(
  data.filter((obj) =>
    Object.values(obj).every((value) => input.includes(value))
  )
);

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

Exploring the process of implementing functions on buttons in Three.js

I have a written program in Three JS that I want to enhance by adding an animated function triggered by a button click event. Additionally, I need help with setting buttons in an inner window and calling all animations on button click events. Any assistanc ...

Deciphering click event parameters in an AngularJS application

Currently, I am in the process of improving a feature in an existing application that has already been deployed. Unfortunately, all the JavaScript code is minified, and I only have access to the HTML files. I need to implement a function that triggers when ...

Launch a modal window that displays a form generated using ngx-formly, which is nested within another form also created with

I have been utilizing ngx-formly to generate Angular forms dynamically from JSON data, and it has been working smoothly. One interesting scenario I encountered involves a custom button on a form that needs to open a modal dialog containing another form upo ...

I am unable to organize an array

I stumbled upon a discussion about clearing an array in JavaScript, but unfortunately I can't participate there. So, here's my query: I have the following code snippet: sumarray.length=0; sumarray = []; for (var i=0; i<3; i++) sumar ...

The Angular 1.5 directive utilizes one-way binding to seamlessly update the parent scope

I am experiencing an issue with a directive that has an isolated-scope and one-way binding variable. Despite setting up the directive to keep the scope separate, any changes made to the variable in the directive controller also update the parent scope. Be ...

Converting a JSON object into an array with JQ

Looking to transform a map of objects into an array using jq Input: { "fish-chips": { "likeDislikeRatio": ["80%", "20%"], "country": ["BRIT_FOOD","USA_FOOD"] }, "saus ...

Encountering a problem with AngularJS - receiving the [ng:areq] error, for more information visit http://errors.angularjs.org/1.3.2/ng/areq

While working on my CRUD angularApp, I encountered a strange error in Chrome dev tools. Here is the complete error message: error: [ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=DbController&p1=not%20a%20function%2C%20got%20string at angular.j ...

Looking to display an alert message upon scrolling down a specific division element in HTML

In the midst of the body tag, I have a div element. <div id="place"> </div> I'm trying to achieve a scenario where upon scrolling down and encountering the div with the ID "place", an alert is displayed. My current approach involves che ...

npm ERROR: Unable to install the package named "<packageName>" because it conflicts with an existing package of the same name

Currently, I am attempting to incorporate the jsonfile package into my project. However, I am encountering a couple of errors: An issue arises when attempting to install a package with the same name as another package within the same directory. (Despite ...

Obtain value of dropdown selection upon change

What is the best way to retrieve the selected value in a drop-down menu when the ID dynamically changes with each refresh? Is it possible to access the particular selected value even when the ID changes? <select name="status" id="dropdown_status3352815 ...

Executing a Python function on a server from a local machine with the help of AngularJS

I am encountering an issue with calling a python function using an angularjs $http request. The python function I have on the server looks like this: import cgi, cgitb data= cgi.FieldStorage() name = data.getvalue("name"); age = data.getvalue("age"); def ...

Is it possible in Elasticsearch to dynamically construct and send a JSON query object?

Currently I am utilizing angularjs alongside elasticsearch.angular.js. Constructing a dynamic JSON query object to reflect user requests has been successfully achieved. I am now seeking assistance on how to pass this constructed object to the search API wi ...

Is there a way to retrieve a collection of files with a particular file extension by utilizing node.js?

The fs package in Node.js provides a variety of methods for listing directories: fs.readdir(path, [callback]) - This asynchronous method reads the contents of a directory. The callback function receives two arguments (err, files), with files being an arra ...

The .each function in JavaScript is creating conflicts with other classes on the webpage

HTML <ul class="courseDates"> <li class="dateOne col-sm-2"> {tag_course date 1} </li> <li class="dateTwo col-sm-2"> {tag_course date 2} </li> <li class="dateThree col-sm-2"> { ...

include the ReactToastify.css file in your project using the import statement

Error in file path C:\Users\User\Documents\GitHub\zampliasurveys_frontend\node_modules\react-toastify\dist\ReactToastify.css:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){:ro ...

Scope challenges with making multiple rest calls in Angular.js

As a beginner in Angular.js, I am facing an issue with $scope not receiving additional value from one of two $resource rest calls. Below is the code snippet: controller: function ($scope, $modalInstance, $route) { $scope.server = {} ...

How Can You Create a Sliding List Animation (Up/Down) Using Angular and Twitter-Bootstrap?

Hey, can you give me a hand with this? :) I'm attempting to create a sleek sliding up and down list in Angular, but I'm struggling to make it work. My CSS skills are not very advanced, so I'm still learning and gave it my best shot: http:// ...

What is the process for a client to determine the data type of a JSON RestResponse?

While working on a client application that interfaces with our existing REST services, there is a decision to be made between using JSON or XML responses. The XML responses are defined by XSD files which provide schema information. By utilizing these XML ...

Discover the Practical Utility of Maps beyond Hash Tables in Everyday Life

I am currently attempting to explain the concept of Maps (also known as hash tables or dictionaries) to someone who is a beginner in programming. While most people are familiar with the concepts of Arrays (a list of things) and Sets (a bag of things), I ...

What is the best way to trigger an AJAX function every 15 seconds?

As part of my web application, I have implemented a JavaScript function that is triggered by the <body onload> event. Within this function, there is a while loop that continuously iterates until it receives the desired response from a PHP page. Unfo ...