Angular 5 - Implementing "similar to %" Filter (similar to SQL)

I have created a method in my code to filter an array based on a specific type.

 filterByType(type: string) {
console.log("Filtering by type");
this.filteredArray = null;
this.filteredArray = this.mainArray.filter((item: Item) => item.type === type);
this.mainArray = this.filteredArray;
console.log(JSON.stringify(this.filteredArray));
console.log("Updated array: " + JSON.stringify(this.filteredArray));

}

How can I implement a 'LIKE' operator as in SQL, such as 'var%'?

Answer №1

Unfortunately, the specific operator mentioned does not exist in JavaScript. However, an alternative solution is to utilize the indexOf() method, which can achieve a similar outcome when used correctly.

filter(typ: string) {
  console.log("Filter");
  this.nagelplattenFiltered == null;

  this.nagelplattenFiltered = this.nagelplatten.filter((nagel: Nagelplatten) => 
  nagel.Bezeichnung1.indexOf(typ) > -1);

  this.nagelplatten = this.nagelplattenFiltered;
  console.log(JSON.stringify(this.nagelplattenFiltered));
  console.log("new: " + JSON.stringify(this.nagelplattenFiltered));
}

By implementing this filter, you will now receive all Nagel objects that include the specified type string.

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

Launching a Material UI Modal nested within a parent component

I have a table displaying various teams. Each row in the table has a menu option that, when clicked, should open either a modal or a dialog box. I want to keep the table, menu functionality, and modals as separate components for better organization. Here&a ...

Is it possible to incorporate a foreign key into my primary key in MySql?

Is something like this structure valid? These columns are in the ACCOUNTS table: user_id (Primary Key) user_name user_pass user_email user_date Accounts can have multiple characters. I want to create a CHARACTERS table like this: character_ID user_ID ( ...

What is the syntax for creating a link tag with interpolation in Angular 2 / Ionic 2?

As I work on developing an app using Ionic 2/Angular 2, I have encountered a challenge that I am struggling to overcome. Let me provide some context: I am retrieving multiple strings from a webservice, and some of these strings contain links. Here is an e ...

Adding fresh 'observers' to a list of 'observers' while the application is running and monitoring updates by utilizing combineLatest in Angular

In my current scenario, I am facing a challenge of managing a list of observables that are constantly being updated by adding new observables to it during the application's runtime. Within this list, I am using combineLatest to perform API calls wit ...

Issue with ReactJS Typescript: Cannot assign type 'number' to type '0, 8, 16, 24, 32, 40, or undefined'

I am looking to implement a grid from material-ui in react using typescript. You can view the live demo here. I have made adjustments to the example to make it work with typescript. Here is how my demo.jsx file looks like: import { withStyles } from &apo ...

A problem arises when the React effect hook fails to trigger while utilizing React Context

I have created a component that is supposed to generate different pages (one for each child) and display only the selected page: import * as React from "react"; export interface SwitchProps { pageId: number; children: React.ReactChild[]; } ...

CPU usage spikes after launching a Cordova project in Visual Studio 2015 RTM

If you're looking for the source code of the project, you can find it at https://github.com/Yaojian/Ionic-TypeScript-Starter/. I decided to create a Visual Studio project by forking https://github.com/Justin-Credible/Ionic-TypeScript-Starter/ and fol ...

Leveraging Angular App with the Bootstrap .modal() JQuery method

I'm encountering issues with Bootstrap 4 and NPM/Angular CLI when using the .modal methods Error: TypeError: $(...).modal is not a function Main.ts: import * as $ from 'jquery'; import * as bootstrap from 'bootstrap'; App.compo ...

The Issue with NG-Zorro Datepicker Manual Input Mask Functionality未分类

Is there a way to mask the datepicker so that users can enter dates in the format MM/dd/yyyy without typing the "/" character manually? I've tried using ngx-mask but it doesn't seem to work for the datepicker component. I have provided a link to ...

Jsx Component fails to display following conditional evaluations

One issue I am facing is having two separate redux stores: items (Items Store) quotationItems (Quote Items). Whenever a product item is added to quotationItems, I want to display <RedButton title="Remove" />. If the quotationItems store i ...

Sign up for a feature that provides an observable exclusively within an if statement

There is an if clause in my code that checks for the presence of the cordova object in the window global object. If cordova is present, it will make a http request and return the default angular 2 http observable. If the application is in a web context wh ...

Converting a JavaScript function to TypeScript with class-like variables inside: a step-by-step guide

During the process of converting a codebase to TypeScript, I encountered something unfamiliar. In particular, there are two functions with what appear to be class-like variables within them. The following function is one that caught my attention: const wai ...

Dynamically assigning column values based on object properties

I am currently utilizing the Ionic Framework along with its grid system that is reminiscent of Bootstrap. However, I believe my query leans more towards AngularJS than specific Ionic components. Here is what I have: <ion-col *ngFor="let col of row ...

What is the best way to combine two functions for the "value" attribute in a TextField?

How can I make a TextField force all uppercase letters for the user when they type, while also storing the text inputted by the user? I have managed to make the TextField display all uppercase letters, but then I can't submit to Excel. On the other ha ...

Error message: "Function call failed in Angular model getter"

Here is the structure of my model: export class User { public username: string; private email: string; constructor() { this.username = undefined; this.email = undefined; } public getUsername(): string { return this.u ...

Having trouble with my Angular 4 application when trying to make a POST request to a

I am struggling to create a straightforward login verification process using Angular 4, PHP, and MySQL. Currently, I can successfully send the user details to PHP but I am unable to retrieve the status afterward, and I'm not sure why. Basically, I ne ...

Extract the section of the string that is between the first and last occurrence of the character '-'. Only keep the middle portion of the string that has more than one character '-'

I found the following entries in my database under the varchar data type: 5-359-258756-54 2-456-58994-85 4-458 -478698-42 5-876-5878-26 My goal is to remove the first number with the following hyphen, the last two numbers with the preceding hyphen, and an ...

What is the process of importing types in TypeScript?

Here is the export I am working with: import * as jwt from 'jsonwebtoken'; ... export type MyJsonWebToken = typeof jwt; Next, when attempting to use it in my code: export class AuthService { constructor( @Inject(constants.JWT) private ...

Declaring Objects and Relationships in Angular Models

Wondering if it's possible to declare an object inside my model. First attempt: export class Employee{ emp_id: number; emp_fname: string; emp_lname: string; emp_birth: string; emp_status: string; emp_photo: string; emp_dep ...

Manipulating strings in Angular using property binding

Is there a way to concatenate the following two statements that work individually? <app-output [warnMessage]="myvalue | number:'1.0-1'"></app-output> <app-output [warnMessage]="'TEXT01' | translate"></app-output&g ...