Steps for confirming whether each element in the array includes the specified search string using Typescript and protractor


How can I verify if each element in an array contains a specific search string in Typescript/Protractor? The issue I faced was that the console statements were returning false because they searched for exact matches instead of the search string. Any suggestions on how to solve this?


arr = [ 'Citibank, N.A.', 'Citi China Companies', 'Citibank Ireland' ] 
search string = 'citi'

Then('I enter the search text where the highlighted results will include a Client Company Name {string}, {string}', async (searchText, companyName) => {
    await acctmgrclientselection.deleteSearchText().then(async () => {
        await acctmgrclientselection.getSelectClientSearchInputEl().sendKeys(searchText).then(async () => {
            await acctmgrclientselection.getSelectClientSearchInputEl().sendKeys(protractor.Key.ENTER).then(async () => {
            await dashboardFilter.getEmployeeListGrid().count().then( async ( CountVal ) => {
                if(CountVal >1)
                {
                    var strArr: Array<string> = [];
                    await acctmgrclientselection.getClientTblCompanyName().getText().then(async (text) => {
                        await strArr.push(text)
                        
                        var sortable = [];
                        strArr.forEach(value => {
                            sortable.push([value]);
                            let sorted_array: Array<string> = sortable.map(arr => arr[0])
                            let result = sorted_array.every(element => element.includes(searchText))
                            console.log(result)    
                        });
                    });
                }
			    else 
                {
           //clear criteria
                    console.log('clear criteria');
                    await element(by.cssContainingText('mat-card.empty-results.mat-card p','0 results matching your criteria')).isDisplayed().then(async()=>{
                        await element(by.cssContainingText('mat-card.empty-results.mat-card a','Clear Criteria')).isDisplayed();
                    });
				}
			});
		});
	});
});
});

Answer №1

Make sure to verify the regular expression with the specified search string.

const pattern = new RegExp("City");
const match = sample_array.every(item => pattern.test(item));
console.log(match);

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

Create a debounced and chunked asynchronous queue system that utilizes streams

As someone who is new to the concept of reactive programming, I find myself wondering if there exists a more elegant approach for creating a debounced chunked async queue. But what exactly is a debounced chunked async queue? While the name might need some ...

How can I modify the join() function of an Array<MyType> in Typescript to instead return MyType instead of a string?

I am working with a specialized string type called MyType = string & { __brand: 'mytype' }. Is there a way to define an override for the Array.join method specifically for arrays of type Array<MyType> so that it returns MyType instead of s ...

steps for transferring a shallow copy of an array to a function

How can I adjust this code so that when I press each button, the console.log displays a different slice of the array instead of always showing me the last 20 elements? for (var i = 0; i < array.length; i++) { var b; var NewArr = []; ...

Sorting an array of numbers using Javascript

Does anyone know how to properly sort an array in JavaScript? By default, the sort method doesn't work efficiently with numbers. For example: a = [1, 23, 100, 3] a.sort() The sorted values of 'a' end up being: [1, 100, 23, 3] If you hav ...

Which specific files do I have to edit in order for Laravel to acknowledge a new data type?

Currently, I am honing my skills in Laravel by working on a Laravel Breeze application. One task that I have set for myself is to incorporate a postal code field into the existing User model, including the registration form. To tackle this challenge, I dec ...

Implementing vue.js to insert a new user into the user array stored in a JSON file

I'm encountering an issue with vue.js where I am unable to successfully add a new user to the user array. Despite console.logging "this.user" when calling the function on submit, I am not seeing a new box with the new user or the new user being added ...

Identifying the various types in Typescript

In the process of developing a solution for Excel involving data from an Office API, I encountered the challenge of distinguishing between different types that a function can return. Specifically, the data retrieved as a string may belong to either a "Cell ...

Hiding Clear button in Autocomplete to display only text

Exploring react-virtualization and Autocomplete features here. I currently have a list set up to display multiple texts when a checkbox is selected. Here's the code snippet: https://codesandbox.io/s/material-demo-forked-1qzd3?file=/demo.tsx The goal ...

Ionic app on mobile device experiencing issue with Autocomplete feature not filtering correctly in Angular

I am facing an issue with my autocomplete form. It works perfectly fine locally, but once compiled to a PWA, the data filtering feature stops functioning properly. The API is returning a JSON array response as expected. var modify = function (term) ...

What is the best way to extract a single parameter from each element of a .json array using Python filtering?

Currently facing an issue with filtering an array obtained from the CoinGecko API. Here's how the array is structured: [ { "id": "01coin", "symbol": "zoc", "name": "01coin" }, ...

Is it possible to optimize the performance of my React and TypeScript project with the help of webpack?

I am working on a massive project that takes 6 to 8 minutes to load when I run npm start. Is there a way to speed up the loading process by first displaying the sign-in page and then loading everything else? ...

The property of undefined map cannot be read

import React from 'react'; import { Card, Text } from "react-native-paper"; import { SafeAreaView, } from "react-native"; class HackerNewsClone extends React.Component { constructor(props) { super(props); this.sta ...

Creating a dynamic array in Simulink with the help of MATLAB functions and Model Explorer

I am facing a challenge while working on a MATLAB Simulink design. I want to create a dynamic uint8 array with a random length. In order to achieve this, I have written a Matlab function with the following variable definition: len = 100; ethMesg = ones([1, ...

Having trouble importing .task files in a Next.js project with TypeScript?

I encountered an issue when trying to import a model.task file into my App.tsx file. After training a hand gesture recognition model in Python, I exported it to a model.task file. Now, I am attempting to import this file into my Next.js + Typescript proje ...

Issue with setInterval function execution within an Angular for loop

My goal is to dynamically invoke an API at specific intervals. However, when attempting to utilize the following code snippet in Angular 7, I encountered issues with the interval timing. I am seeking a solution for achieving dynamic short polling. ngOnIn ...

What is the best way to search for and isolate an array object within an array of objects using Javascript?

I am attempting to narrow down the list based on offerings const questions = [ { "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]}, { "id": 1505, "offerings": [ ...

Encountering a TypeScript error while calling a Vue lifecycle hook method

Struggling to call a method in a Vue root component from a lifecycle method in typescript? See below for a simple example that showcases this issue: import Vue from "vue"; class Game { a: number; b: number; constructor() { this.a = 3; ...

In TypeScript, the first element of an array can be inferred based on the second element

Let's consider a scenario where we have a variable arr, which can be of type [number, 'number'] or [null, 'null']. Can we determine the type of arr[0] based on the value of arr[1]? The challenge here is that traditional function ov ...

Generating a JSON file dynamically containing hierarchical data

I am looking to create a JSON file structure similar to the example provided in this post, but I need it to be dynamic. The current solution is specific for "entry1" and "entry2", however, I want the flexibility to use any entity name such as customer, add ...

Managing data with Angular 2: Setting and retrieving values

In my current project, I am working on developing a service that can parse data to different components based on various routes. When I call this service within the same component, everything works as expected and I get the desired results. However, when ...