How to search for fractions of a word within an array in angular

With the following code I can filter the information of an array as long as the parameters sent are equal to what was evaluated within the array, now what I need to do is be able to search within the array for a fraction of some word

Within the information that the arrangement has, I save the name of each representative, let’s imagine that we have several representatives with their first name Juan, I want that when I pass the word Juan it brings me all the representatives with this name

/asignando las informaciones del localstorage al arreglo

this.signinmodel = this.localStorageService.getData('TablaRepresentantes') || [];
 //Filtrar por nombre
   this.data = this.signinmodel.filter(item => item.repCodigo === this.repcodigo && item.repClave === this.repclave  );

one way could be:

function arrayContainsFractionOfString(arr, fraction) {
  return arr.some(element => element.includes(fraction));
}

const containsFraction = arrayContainsFractionOfString(myArray, fractionToCheck);

console.log(containsFraction);

example of the implemented code


// Example of an array of strings
  myArray: string[] = ['apple', 'banana', 'cherry', 'date'];

  
  // Function to check if any string in the array contains a specific fraction
  arrayContainsFractionOfString(arr: string[], fraction: string): boolean {
    return arr.some(element => element.includes(fraction));
  }

  // Example of use
  verifyFraction() {
    const fractionAReview = 'na';
    const containsFraction = this.arrayContainsFractionOfString(this.stringArray, fractionAReview);

    if (containsFraction) {
      console.log(`The array contains a fraction of "${fraccionARevisar}"`);
    } else {
      console.log(`The array does not contain a fraction of "${fraccionARevisar}"`);
    }
  }

If the array contains the data in this way as can be seen in the first example, the solution provided is fully functional,
but the array data has the following structure

stringArray: any[] = [
    { title: "Green banana", color: "green" },
    { title: "brown banana", color: "rotten" },
    { title: "Yellow Banana", color: "Yellow" },
    { title: "Red banana ", color: "cannot be eaten" },
    { title: "Apple", color: "Red" },
    { title: "Guava", color: "Green" },
    { title: "Strawberry", color: "Red" }
  ];

I get the following error

core.mjs:8400 ERROR Error: Uncaught (in promise): TypeError: element.includes is not a function
TypeError: element.includes is not a function
at client.page.ts:227:40
at Array.some ()
at ClientPage.arrayContainsFractionOfString (client.page.ts:227:16)
at ClientPage.verificarFraccion (client.page.ts:233:35)
at ClientPage.ngOnInit (client.page.ts:65:11)
at callHook (core.mjs:2434:22)
at callHooks (core.mjs:2403:17)
at executeInitAndCheckHooks (core.mjs:2354:9)
at refreshView (core.mjs:10341:21)
at detectChangesInternal (core.mjs:11529:9)
at resolvePromise (zone.js:1211:31)
at resolvePromise (zone.js:1165:17)
at zone.js:1278:17
at _ZoneDelegate.invokeTask (zone.js:406:31)
at core.mjs:23896:55
at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:23896:36)
at _ZoneDelegate.invokeTask (zone.js:405:60)
at Object.onInvokeTask (core.mjs:24197:33)
at _ZoneDelegate.invokeTask (zone.js:405:60)
at Zone.runTask (zone.js:178:47)

solution

// Función para filtrar el arreglo basado en una fracción de la propiedad 'title'
    filtrarPorFraccion(fraccion: string): any[] {
    this.clientmodel = this.localStorageService.getData('Clientes') || [];
    
    return this.clientmodel.filter(item => item.cliNombre.toLowerCase().includes(fraccion.toLowerCase()));
    }
  
    // Ejemplo de uso
    filtrarYMostrar() {
      debugger;
      const fraccionAFiltrar = 'COLMADO';
      const resultadoFiltrado = this.filtrarPorFraccion(fraccionAFiltrar);
  
      console.log(`Elementos con la fracción "${fraccionAFiltrar}":`, resultadoFiltrado);
    }

yes you must tweak it a little to make it work with the kind of data you need

that was to show a “prototype” :slight_smile:

1 Like