'How do I filter an array with TypeScript in Angular?
I have this code
// Common
import { Pipe, PipeTransform } from '@angular/core';
// Interface
import { IQuestion } from '../../models/question.interface';
@Pipe({
name: 'tableFilter',
})
export class TableFilterPipe implements PipeTransform {
transform(list: Array<IQuestion>, value: string) {
const filterWords: Array<string> = value
.split(/[. ,;\\\/]/)
.filter((word) => {
const isNumber: boolean = Number.isInteger(Number(word));
const isAvailableLength: boolean = word.length > 2;
const isLk: boolean = /^ЛК$/i.test(word);
if (isNumber || isAvailableLength || isLk) {
return true;
}
})
.map((word) => word.slice(0, 3).toLowerCase());
return value
? list.filter((tableRow) => {
const isEveryWordExistInQuestion: boolean = filterWords.every(
(word) => tableRow.question.toLowerCase().includes(word)
);
const isEveryWordExistInAnswer: boolean = filterWords.every((word) =>
tableRow.answer.toLowerCase().includes(word)
);
const isEveryWordExistInNumber: boolean = filterWords.every(
(word) =>
tableRow.category.number.toString() === word ||
tableRow.product.number.toString() === word ||
tableRow.number.toString() === word
);
if (
isEveryWordExistInQuestion ||
isEveryWordExistInAnswer ||
isEveryWordExistInNumber
) {
return true;
}
return false;
})
: list;
}
}
The search goes through 3 columns. Questions, Answers and question number.
How to make it search for each letter dynamically from similar (i++) in order not to depend on the length of the word you are looking for
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
