The issue arises when attempting to use the search feature in Ionic because friend.toLowerCase is not a valid function

I keep encountering an error message that says "friend.toLowerCase" is not a function when I use Ionic's search function. The unique aspect of my program is that instead of just a list of JSON items, I have a list with 5 properties per item, such as friend.name, friend.status, and so on. I made some modifications like changing Ionics list to items and mine to friendsList. I suspect the error might be due to naming confusion, but I haven't been able to pinpoint the mistake.

HTML

<ion-searchbar (ionInput)="getFriends($event)"</ion-searchbar>
...
<ion-item *ngFor="let friend of friendsList">
...
<h2>{{friend.name}}</h2>
...

TS

 export class RankfriendsPage {
      friendsList;

     constructor(public navCtrl: NavController, public navParams: NavParams) {
       this.initializefriendsList();
  }

  initializefriendsList() {
    this.friendsList = [
     {
          "name": "Alice",        //list example
          "status": "Online",
          "img": "img/6.jpeg",
          "img2": "img/33.jpeg",
          "text": "+ADD"
        },
    ];

      }




       getFriends(ev) {
          // Reset items back to all of the items
          this.initializefriendsList();

          // set val to the value of the ev target
          var val = ev.target.value;

          // if the value is an empty string don't filter the items
          if (val && val.trim() != '') {
            this.friendsList = this.friendsList.filter((friend) => {
              return (friend.toLowerCase().indexOf(val.toLowerCase()) > -1);
            })
          }
        }

Answer №1

The problem arises from the absence of a friend object that possesses the function toLowerCase(). It appears that you require,

 this.friendsList = this.friendsList.filter((friend) => {
    return (friend.name.toLowerCase().indexOf(val.toLowerCase()) > -1);
 })

Answer №2

friend = {
  "name": "Bob",
  "status": "Offline",
  "img": "img/8.jpeg",
  "img2": "img/57.jpeg",
  "text": "+ADD"
}

It is noticeable that there is no toUpperCase property included in the object.

Should you require it, apply it to a string like friend.name.

Answer №3

Pal is considered an object and objects do not possess the toLowerCase() method.

The toLowerCase() method can only be applied to strings.

You may utilize friend.status.toLowerCase(), friend.name.toLowerCase(), etc.

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 difficulty extracting subarray from PHP json_decoded array

After receiving a JSON response, I successfully converted it into an array using json_decode. When I use var_dump to inspect the array, I get the following output: array(1) { ["FlightInfoResult"]=> array(2) { ["next_offset"]=> in ...

typescript - transforming text into numerical values

newbalance = (Number(this.balance)) + (Number(this.pastAmount)); The result for my newbalance calculation is coming back as undefined, even though this.balance is 34 and this.pastAmount is 23. I've set this up in the controller and I'm trying t ...

Converting an array of objects into a TypeScript dictionary with IDs as the key mapping

Is there a way to provide type hints for better autocompletion when accessing keys like dictionary.Germany from the following data and types? type Entry = { tld: string; name: string; population: number; }; const data: Entry[] = [ {tld: 'de&a ...

Next.js: Generating static sites only at runtime due to getStaticProps having no data during the build phase, skipping build time generation

I am looking to customize the application for individual customers, with a separate database for each customer (potentially on-premise). This means that I do not have access to any data during the build phase, such as in a CI/CD process, which I could use ...

It seems that every time you save data in Angular, the local storage array gets overwritten

While using Angular, I encountered an issue with saving to local storage. The code works fine for saving items initially, but on refreshing the page and trying to add more objects to the local storage array, it overwrites instead of appending. Can you help ...

Guide on building an npm package that seamlessly allows for installation both locally and globally (-g) using webpack and typescript

As I work on developing an npm package with options for both local and global (-g) installations, I find myself puzzled by the distinctions between the src and lib directories and the purpose of the bin directory. In my previous projects, I typically util ...

How can I utilize a single component across several pages in Angular for optimal efficiency?

I am currently in the process of developing a website that will have consistent formatting for the 'about us,' services, and contact us sections. The desired format includes: A banner photo A main heading A description Instead of creating sepa ...

Utilizing Embedded Jetty for Angular URL rewriting

While deploying a WebSocket and an Angular application with Jetty, everything works fine during development. However, I am facing an issue in production where refreshing the frontend or entering a URL results in a 404 error from the server indicating that ...

What is the best way to convert a number into a string format for JSON formatting?

I am struggling with this code that I wrote to output data in JSON format for Google Chart. However, the issue I'm facing is that the return value for numbers is being converted into strings and cannot be read by Google Chart. $rows = array(); foreac ...

Adjusting the value of a mat-option depending on a condition in *ngIf

When working with my mat-option, I have two different sets of values to choose from: tempTime: TempOptions[] = [ { value: 100, viewValue: '100 points' }, { value: 200, viewValue: '200 points' } ]; tempTimesHighNumber: TempOpt ...

Creating a magical narrative within an NX workspace with the help of a user interface library

Within my NX workspace, I have multiple applications and libraries. As someone new to Storybook, I followed the instructions found here: in order to incorporate Storybook support into a simple component library. This library consists of two Angular module ...

Utilize Array.push to add 2 new rows to a table using Angular 4

I have two arrays that are almost identical, except for two items which are the fakeDates: this.prodotti.push({ idAgreement: this.idAgreement,landingStatus: this.landingStatus, landingType: this.landingType, startDate: this.startDate, expirationDate: thi ...

What is the best way to format dates in jQuery AJAX requests?

Utilizing jquery to interact with a REST API in (ASP.net Web API). I've constructed a basic object and then utilized jquery to 'PUT' it on the server: var myObject = { name: 'just a name', createDate: new Date() } $.ajax({ ...

Using TypeScript with Angular-UI Modals

Currently, my goal is to create a modal using angular-ui-bootstrap combined with typescript. To begin, I referenced an example from this link (which originally utilizes jQuery) and proceeded to convert the jQuery code into typescript classes. After succes ...

A guide on initializing an Angular 2 component and setting public properties

My service is returning the expected data when injected into a component. However, I am encountering an issue where the response remains undefined when I subscribe to the service method and assign it to a public variable during initialization of the compon ...

Different tsconfigs assigned to various directories

In my project, I am using Next.js with TypeScript and Cypress for E2E tests. The challenge I am facing is configuring tsc to handle multiple configs for different folders. The tsconfig.json file in the project root for Next.js looks like this: { "c ...

Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this: <input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">, and display the result in another textbox ...

Add a class to a button in an Angular btn-group if a specific string is found within an

I am currently working on a project where I have multiple buttons that need to toggle an active class when selected in order to change their color. Below is a snippet of what I have: In the array called 'selected', I have: this.selected = [&ap ...

Encountering Issue: NG0303 - Unable to associate 'ng-If' as it is not recognized as a valid attribute of 'app-grocery'

NG0303: Encountering an issue with binding to 'ng-If' as it is not recognized as a valid property of 'app-grocery'. A similar problem was found here but did not provide a solution Despite importing CommonModule in app.modules.ts, I am ...

Encountering an Uncaught Error: MyModule type lacks the 'ɵmod' property

I am currently working on developing a custom module to store all my UI components. It is essential that this module is compatible with Angular 10 and above. Here is the package.json file for my library: { "name": "myLibModule", &qu ...