Tips for effectively finding information within a table

I came across this code snippet for a table created with sveltestrap. I am struggling to figure out how to make the search case insensitive and would appreciate any help or guidance on how to do so. This is the code I have tested using Svelte REPL:

    <script>
    import { onMount } from 'svelte';
    import { Card, CardBody, CardHeader, Input, Table, Column, Styles } from 'sveltestrap';
    
    let search = undefined;
    let users = [];
    $: visibleUsers = search ?
        users.filter(user => {
            return  user.name.first.match(`${search}.*`)  || user.name.last.match(`${search}.*`)
        }) : users;

    onMount(async () => {
        const resp = await fetch('https://randomuser.me/api?results=25&inc=id,name,email,')
        const data = await resp.json();
        users = data.results;
    });
</script>

    <style>
      @import url('https://gthomas-appfolio.github.io/bootstrap-coastline/bootstrap-coastline.css');
    </style>
    
    <Card>
        <CardHeader>
            <Input type="search" bind:value={search} class="ms-auto w-auto" placeholder="Search" />
        </CardHeader>
        <CardBody>
            <Table striped rows={visibleUsers} let:row={user}>
                <Column header="uuid">
                    {user.id.value}
                </Column>
                <Column header="First">
                    {user.name.first}
                </Column>
                <Column header="Last">
                    {user.name.last}
                </Column>
                <Column header="Email">
                    {user.email}
                </Column>
            </Table>
        </CardBody>
    </Card>

Answer №1

This piece of code is leveraging the power of regular expressions in order to perform matching:

        users.filter(user => {
            return  user.name.first.match(`${search}.*`)  || user.name.last.match(`${search}.*`)
        }) : users;

Regular expressions can be modified with flags, such as the "i" flag for case insensitivity. By using the RegExp constructor, you can dynamically create a regular expression along with any necessary flags.

To enhance the above code snippet, you could make the following adjustment:

        users.filter(user => {
            const regex = new RegExp(`${search}.*`, 'i')
            return  user.name.first.match(regex)  || user.name.last.match(regex)
        }) : users;

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

Adding an image using Jquery to a particular set of coordinates

Is there a way to insert an image at the exact spot where a user clicks inside a div using jQuery? I am working on creating a simple game that requires users to click on the screen to guess a position - and have an image appear wherever they click... Whi ...

Instructions for inserting text into a donut chart created with Google, utilizing basic HTML, JavaScript, or jQuery syntax

Are there any attributes in Google Chart that I should know about? I am just starting to use Google Charts and would appreciate some guidance. Here is the code snippet I am working with: function drawChart() { var data = google.visualization.arrayTo ...

Incorporating a division using the data() method

Currently, I am in the process of generating a list of flash SWFs. The data for this list is retrieved through an ajax call, which returns a JSON object. To populate the rows with data, I utilize my makeAppRow function. makeAppRow = function(myData) { ...

Angular JS effectively prevents redundant data from being displayed to users when scrolling infinitely while also efficiently removing DOM elements for previous data

I'm currently working on implementing AngularJS Infinite Scroll without using jQuery. My goal is to display the first 3 data items when the page loads and then load the next 3 data items from a JSON array object as the user scrolls. The issue I am fac ...

Vue is set up to monitor changes in two connected input fields for user input exclusively

Two input fields are available, where the value of one can affect the other. If a value between 1 and 200 is entered in the level field, Vue will look up the corresponding points for that level and populate the points field with them. Conversely, if a us ...

When using ngClick with a parameter, the parameter is not being successfully passed

My table resembles a tree structure with two ng-repeats. <table> <tr ng-repeat-start="am in anArray"> <td><button ng-click="TheFunction(am)"></button></td> </tr> <tr ng-repeat-start="em in anotherArray"> < ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

The JavaScript function's argument is determined by the value submitted in EJS

I am currently in the process of developing an express application. One of the key features involves a drop-down menu where the selected value is sent to users.js to trigger a MongoDB query. The results of this query are then returned to the EJS template a ...

How can I verify the presence of email and mobile numbers in my MongoDB database?

const express = require('express'); const router = express.Router(); require('../db/conn'); const User = require('../model/userSchema'); router.get('/', (req, res) => { res.send(`Hello World from the server ...

Complete picture in a circular div with aspect ratio

I'm currently working on creating a profile page and I'd like to have an image inside a circular div. The challenge is that I want the image to maintain its aspect ratio, even though the dimensions are unknown and users can upload images of any s ...

Horizontal scroll functionality featured in a D3 bar graph

I'm currently working on implementing a bar graph with a selection dropdown that includes 3 values: By Date, By Week, By Month (where 'By Date' is the default option). When retrieving data from the backend for 'ByDate', I have a l ...

Extracting and retrieving data using JavaScript from a URL

After reviewing some code, I am interested in implementing a similar structure. The original code snippet looks like this: htmlItems += '<li><a href="show-feed.html?url=' + items[i].url + '">' + items[i].name + '& ...

Broken Mui Input - Full width with attributes for minimum and maximum values

I've created a sandbox to demonstrate an issue I came across that seems unbelievable, but it's happening. Here's the link: https://codesandbox.io/s/nifty-swanson-yxj4n2?file=/NumberField.js:1075-1097 The problem is simple - when both the ht ...

Use underscore.js to flatten an object structure into a key-value pair array of children

After analyzing the given data structure: var data = [{ name: "Some Name", id: 1, children: [ { name: "prop1", value: 1 }, { name: "prop2", value: 2 }, { name: "prop3", value: 3 } ] }, { name: "Some Other Name", ...

Ways to retrieve slider value when button is clicked?

I am currently working on a range-slider that has two ranges and I need to retrieve the slider value in my javascript code. Here is my approach so far: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.cs ...

image background not appearing in HTML, CSS, and JavaScript game

When working on a simple HTML, CSS, and JavaScript game, I decided to make a change in the CSS code. Instead of setting the background color to green as before, I opted to use an image with the following line: background-image: url(flappybird.png); I also ...

Exploring the behavior of Object.assign in for loops and forEach loops

I've come across an interesting anomaly with Object.assign in the following scenario. function sampleFunction(index, myList) { myList.forEach((element, i) => { if (i === index) { console.log(Object.assign({}, {"newKey": " ...

Encountered an issue while attempting to verify email through ajax with the database

After reviewing responses from multiple users here, I am aiming to verify if an email is already in the database. Below is the code snippet I am using: **HTML CODE** <div class="form-group"> <div class="col-sm-12"> ...

Exploring the process of iterating through JSON data using JSON.parse

After making a request to my Django server for data, I utilized JSON.parse to convert the data into a JSON object. My goal is to iterate through each attribute of the object's field and execute the function createDiv on each one. function load_blog( ...

NodeJS Static method is not a valid function

I have encountered an issue with my NodeJS application where I am trying to access a static Method that performs a calculation Function. However, when attempting to access the Method, I received an error stating "isNotAFunction". Below is the static class ...