Locate and refine the pipeline for converting all elements of an array into JSON format using Angular 2

I am currently working on implementing a search functionality using a custom pipe in Angular. The goal is to be able to search through all strings or columns in a received JSON or array of objects and update the table accordingly.

Here is the code snippet of my Custom Pipe (although it's not performing as expected):

transform(items: any[], args:string): any {
    let keys = [];
    for (let key in items) {
            keys.push({key: key, value: items[key]});
    }
    let ans = [];
    for (let k in keys){
        if(items[k].value.match('^.*' + args +'.*$')){
            ans.push({key: k, value: items[k]});
        }
    }
    return ans;  
}   

The search input HTML element :

<input type="text" #filterInput (keyup)="0">

Loading the table data using the custom pipe:

<tbody *ngIf="usersBool">
<tr *ngFor="let entry of content | filterArrayOfObjects: filterInput" >
    <td>{{entry.value.enrollmentId}}</td>
    <td>{{entry.value.firstName}}</td>
    <td>{{entry.value.LastName}}</td>
    <td>{{entry.value.typeOfUser}}</td>
    <td>edit</td>
    <td>delete</td>

</tr>
</tbody>

Dummy content example:

this.content = [
               {
                   "enrollmentId": "A0xxxxxx",
                   "firstName": "Bob",
                   "LastName": "Bob",
                   "typeOfUser": 'Admin'
               },
               {
                   "enrollmentId": "A0xxxxxx",
                   "firstName": "Bob",
                   "LastName": "Bob",
                   "typeOfUser": 'Admin'
               },
               {
                   "enrollmentId": "A0xxxxxx",
                   "firstName": "Bob",
                   "LastName": "Bob",
                   "typeOfUser": 'Admin'
               }
           ];

Answer №1

In order to find a match in my content Dictionary with the input in the search bar, I needed to carefully examine each key in the dictionary

transform(items: any[], args:string): any {

    let ans = [];
    for (let k in items){
        if(items[k].enrollmentId.match('^.*' + args +'.*$')
           || items[k].firstName.match('^.*' + args +'.*$')
           || items[k].lastName.match('^.*' + args +'.*$')
           || items[k].typeOfUser.match('^.*' + args +'.*$')) {
            ans.push({key: k, value: items[k]});
        }
    }
    return ans;

}

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

Need assistance with the Angular polyfill.ts file? Wondering where to place the polyfill code and how to manage it effectively?

Currently encountering an error in my Angular project that requires some 'polyfilling'. Due to the restriction on editing webpack.config.js directly, it seems necessary to work with the polyfill.ts file instead. The issue lies in the fact that An ...

"Enhancing User Experience with Hover States in Nested React Menus

I have created a nested menu in the following code. My goal is to dynamically add a selected class name to the Nav.Item element when hovering, and remove it only when another Nav.Item is hovered over. I was able to achieve this using the onMouseOver event. ...

What is the best way to interweave my objects within this tree using recursion?

I am working on creating a new function called customAdd() that will build a nested tree structure like the one shown below: let obj = [] let obj1 = { key: "detail1Tests", id: "94d3d1a2c3d8c4e1d77011a7162a23576e7d8a30d6beeabfadcee5df0876bb0e" } ...

Tips for including extra items in a JSON String using Angular 2

function execute(req:any): any { var stReq = JSON.stringify(req); // Adding additional item "Cityname": "angular2City" inside req req.Cityname = 'angular2City'; } Now, how can I include the additional item "Cityname": "angular2C ...

Turning an array of strings into a multidimensional array

I have a JavaScript string array that I need to convert into a multidimensional array: const names = [ "local://john/doe/blog", "local://jane/smith/portfolio", "as://alexander/wong/resume" ]; The desired output sh ...

Navigating with the router on a different page

My appcomponent contains all the routes, and on the next page I have several links that are supposed to route to the same router outlet. How can I navigate when a link is clicked? I attempted using [routerLink]="['PersonInvolved']", but I encoun ...

From a series of arrays filled with objects, transform into a single array of objects

Upon using my zip operator, I am receiving the following output: [ [Obj1, Obj2, ...], [Obj1, Obj2, ...] ]. To achieve my desired result, I am currently utilizing the code snippet below: map(x => [...x[0], ...x[1]]) I am curious to know if there exists ...

Show website traffic using TypeScript visitor counter

I need help adding a visitor counter to my website. I initially tried using the API from , but it seems that the server is currently down and I can no longer use it. I found another API at . How can I retrieve count data from this specific API endpoint ...

tips for iterating through a json string

When retrieving data from PHP, I structure the return like this: $return['fillable'] = [ 'field_one', 'field_two', 'field_three', 'field_four', 'field_five', ]; $json = json_ ...

Unable to push items to an array in Angular

I am currently working with a Java service that retrieves data from an Oracle database. The goal is to display the results in an Angular application using an array of objects: resultSet:Info[]=[]; Here is the code for the service: pastHourInfo() { ...

"Troubleshooting: Module not found" (Getting started with Jest in a nested project connected to a shared directory)

I've recently taken over a project that contains the following folder structure: node_modules/ server/ ├── node_modules/ ├── src/ │ └── helpers/ │ ├── updateTransactions.ts │ └── updateTransactions.tes ...

Unable to locate the namespace for the installed library

Looking for a solution in my ExpressJS setup with Pino logger. I am trying to create a class that can be initialized with a Pino logger. Here is the code snippet: import express, { NextFunction, Request, Response } from 'express'; import pino fr ...

Determining the return type based on an optional generic type in TypeScript

I have created a function that generates an object (map) [key] : value from an array. My goal is to make the value method optional, and if not provided, simply return the item as it is. Here is the code I have written: export default class ArrayUtil ...

Encountering empty values during the process of decoding intricate JSON structures

My current project involves utilizing a 3rd party web API that provides a JSON string with specific formatting. The JSON string is enclosed in curly braces and contains various data points for GPS tracking: { "866968030210604":{ "dt_server":"201 ...

Displaying Data in a Tree Structure Using JSON

I am currently working on an Angular project which includes a nested JSON file structured as follows: I would like to import this JSON data from the file and create a treeview populated with its contents. How can I achieve this? { "?xml": { ...

Guide to sending and receiving JSON data using XAMPP PHP

Currently, my XAMPP 7.1.10-0 server is up and running with the following index.php file: <?php if(isset($_POST["username"])) { echo $_POST; header("Location:getbooks.php"); exit; } else { echo file_get_conten ...

While working with AJAX, the variable value remains static and is not refreshed

My jQuery code successfully calls a REST Service and handles the response in the AJAX Success event. However, I'm facing an issue where the variable "SelectedVal" (document.getElementById('Text1').value) is not getting updated with each cli ...

What is the best way to link labels with input fields located separately in Angular?

Imagine a scenario where labels and form fields are being created in a *ngFor loop, as shown below: app.component.ts export class AppComponent { items = ['aaa', 'bbbbbb', 'ccccccccc'] } app.component.html <div class ...

Iterate through a JavaScript array to access objects with varying IDs

Struggling to navigate the JSON data due to ID 29450 and 3000 out of a total of 1500 IDs in the database. Looking to log the information ['Id', 'Description', 'StartDate'] for both IDs. Feeling a bit stuck, hoping someone can ...

Tabulator - Using AJAX to retrieve a JSON document

I am currently working on importing a json file from an Azure blob container using Azure Data Factory. Despite closely following the documentation and researching various resources on Stack Overflow, I am facing challenges with making the ajax request fun ...