Find a specific string within an array where both the keys and values are subject to change

Below is an array that I have:

 var Array = [{id:100,name:'N1',state:'delhi',country:'india',status:'active'},
 {id:101,name:'N2',state:'kenya',country:'africa',status:'suspended'}
 {id:102,name:'N3',state:'kerala',country:'india',status:'inactive'}
 {id:103,name:'N4',state:'victoria',country:'australia',status:'active'}]

I also have a search field where I need to filter the array based on the searched value and return the matching object. The challenge I face is that I do not know in advance what key-value pairs will be present in the array as they are dynamically generated. Additionally, I am looking for suggestions on how to utilize Regex to search the array. It should match each character I type and return the matching object in an array. An example of the expected result is shown below:

search key : ind

[{id:100,name:'N1',state:'delhi',country:'india',status:'active'},
 {id:102,name:'N3',state:'kerala',country:'india',status:'inactive'}]

search key : N2

[{id:101,name:'N2',state:'kenya',country:'africa',status:'suspended'}]

Any advice or suggestions would be greatly appreciated. Thank you.

Answer №1

To efficiently search for specific values within an array, you can utilize a filtering method that directly checks the values based on your criteria. This approach is especially useful when looking for substrings or case-insensitive matches.

function customSearch(value) {
    return data.filter(obj => Object.values(obj).some(val => val === value));
}

var data = [{ id: 100, name: 'N1', state: 'delhi', country: 'india', status: 'active' }, { id: 101, name: 'N2', state: 'kenya', country: 'africa', status: 'suspended' }, { id: 102, name: 'N3', state: 'kerala', country: 'india', status: 'inactive' }, { id: 103, name: 'N4', state: 'victoria', country: 'australia', status: 'active' }];

console.log(customSearch('india'));
console.log(customSearch('N2'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Having trouble loading the linked CSS file in the Jade template

My directory structure is organized as follows: --votingApp app.js node_modules public css mystyle.css views test.jade mixins.jade In the file mixins.jade, I have created some general purpose blocks like 'bo ...

Can I assign a value from the tagModel to ngx-chips in an Angular project?

HTML code: <tag-input class="martop20 tag-adder width100 heightauto" [onAdding]="onAdding" (onAdd)="addInternalDomain($event)" type="text" Ts code: addInternalDomain(tagTex ...

The use of callbacks is ineffective in addressing the asynchronous nature of

Hello everyone, I'm currently working on a weather app and I'm facing an issue with the asynchronous behavior of useState. I've come across some suggestions on Stack Overflow that using a callback in the useState function might solve the pro ...

Troubleshooting port deployment challenges on GCP for a Node.js project

My current Node.js project has one HTTP server running on port 8080, with a service URL generated for it. However, inside the http.createServer function, I have set up another server for an NLP engine that is listening on port 8081. While I am able to ac ...

Can WebDriver (HtmlUnit, Ruby bindings) be configured to bypass JavaScript exceptions?

When attempting to load the page, HtmlUnit throws an exception and crashes my test. caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true) driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps) drive ...

Multipart form data processing without specifying files

I need help with uploading an image using node.js, express, and multiparty. My code is as follows: HTML <!DOCTYPE html> <html> <body> <form method="post" action="/img"> Select image to upload: <input type="file" name= ...

`@keyup.enter event listener will only be triggered upon pressing the Enter key if the focus is on

My login button only seems to work when I press the tab button after entering my email and password. I would like it to work whenever I press the enter key while on the Login page of this VueJS application. This is the HTML template: <template> ...

Tips on choosing and showcasing information from jQuery date and time picker

I am struggling to show the selected data from a jQuery date and time picker and save it to a database using PHP with MySQL. I am not sure how to retrieve the information. Here is the jQuery code for the date and time picker along with a suggested jQuery f ...

What is the best way to add a multiselect option in Django to display detailed information for each selected item

Experience the Description Image I am looking to develop a mini demonstration similar to the image provided. When I click on an item in the left column to select a country, I want the right column to dynamically display all the cities of the chosen countr ...

Guide to setting the first tab as the default tab using Thymeleaf, Css, and Bootstrap

I am currently working on a project where I need to dynamically create tabs based on a list retrieved from my Spring backend using Thymleaf and Bootstrap. While I have managed to successfully create the tabs and content, I am facing an issue where the fi ...

Using JavaScript to Detect Asynchronous Postbacks in ASP.NET AJAX

Seeking advice on the JavaScript code required to determine if an asynchronous postback is in progress. Can anyone help with this? Appreciate any assistance. ...

Jquery code is malfunctioning when used in Next.js, displaying unexpected outcomes; however, it functions properly in React

I am currently working on implementing a UI requirement that involves adding an active class name to the children div one at a time. The process starts by adding the class to the first child, then removing it and adding it to the second child div. This cyc ...

Is there a way to alter the color of a button once it has been clicked?

For a school project, I am working on coding a website to earn extra credit. My goal is to create a fancy design that stands out. One of the main things I am trying to achieve is having a button change color when it is clicked, similar to how it highlight ...

What is the best way to effectively utilize bootstrap and JavaScript together?

I've been trying to implement some JavaScript on top of Bootstrap 5, but it doesn't seem to be working. I'm not sure if I'm doing something wrong with the JavaScript itself, or if there's an issue with how it's interacting wit ...

Getting PHP Post data into a jQuery ajax request can be achieved by using the `$_POST

I'm struggling to figure out how to pass the blog title into the data field of my ajax call. I've been searching for beginner tutorials on SQL, PHP, and AJAX, but haven't found anything that clarifies this issue. If anyone knows of any usefu ...

Tips for handling notifications that have been read and unread in a React application

In my current project using React JS, I am tasked with creating a notifications component that will display account-related activities. The notifications need to be sorted into read and unread categories. My main question is how should I manage the read a ...

What are the steps to activate code suggestion with codemirror?

My goal is to implement CodeMirror for enabling users to input code in various languages like CSS, HTML, and JavaScript. One specific requirement is to provide hints to the user when typing code in CSS mode. div { padding- } The system should prompt th ...

What is the syntax for defining parameters in an overloaded arrow function in TypeScript?

Trying to create an async arrow function that can handle a single image object or an array of image objects. I'm new to TypeScript overloading and may be approaching this the wrong way. Here's what I've come up with: type ImageType = { ...

Ways to display values within input fields

As someone new to coding, I am working on a feature for my website that involves displaying specific values in input boxes based on the selection made from a dropdown menu. For example, if "apples" is selected, values like "1.2" and "4.00" should appear in ...

Unable to access Vue component method beyond its scope

Having an issue with my Vue component that I'm trying to call a method from outside its wrapper element #app. Is there a way to register the component so I can easily call it like Component.function(); var viewController = new Vue({ el: "#app", ...