filter failing to provide output

Issue with fetching partnername from the filter function, always returning undefined.

 administrationList = [
  {
    "runid": 6,
    "partnerid": 2,
    "partnername": "test admin2",
    
  },
  {
    "runid": 5,
    "partnerid": 5,
    "partnername": "test admin1"
  }
]

let selectedPartner = administrationList.filter(item => item.partner_id == 7).partner_name;
console.log("Selected Partner Name:" + selectedPartner); //outputs undefined

Answer â„–1

Utilize filter to retrieve an array instead of an object, then select the first element

let val = this.administrationList.filter(pItem => pItem.partnerid == 7)[0].partnername;

If you prefer to receive the object directly, use the find method

let val = this.administrationList.find(pItem => pItem.partnerid == 7).partnername;

Another option is to employ the reduce function

let val=this.administrationList.reduce(function(prev, curr) { return (curr.partnerid === 3) ? curr.partnername : prev; }, null);

var administrationList = [
  {
    "runid": 6,
    "partnerid": 2,
    "partnername": "test admin2",
    
  },
  {
    "runid": 5,
    "partnerid": 5,
    "partnername": "test admin1"
  }
]

let val = administrationList.filter(pItem => pItem.partnerid == 7)[0]
console.log(val ? val.partnername : null) ;
val = administrationList.find(pItem => pItem.partnerid == 2);
console.log(val ? val.partnername : null) 
val=administrationList.reduce(function(prev, curr) { return (curr.partnerid === 5) ? curr.partnername : prev; }, null);
console.log(val)

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

"Enhancing the functionality of @angular/fire by transitioning from version 6.x to 7.x

I need to update my app dependencies and code from @angular/fire 6.x to 7.1.0-rc4 in order to access a feature that is not available in the 7.0.x version. I made the necessary changes in my package.json as follows: "@angular/fire": "~7.1.0-r ...

What is the process for generating an alert box with protractor?

While conducting tests, I am attempting to trigger an alert pop-up box when transitioning my environment from testing to production while running scripts in Protractor. Can someone assist me with this? ...

Updates to Providers in the latest release of Angular 2

When working with angular 2.0.0-rc.1, we implemented a Provider using the new Provider method as shown in the code snippet below: var constAccessor = new Provider(NG_VALUE_ACCESSOR, { useExisting: forwardRef(() => EJDefaultValueAccessor), ...

A dynamic Angular component consisting of two distinct rows to be seamlessly integrated into an existing table

I'm currently working on a component called "template" that can consist of either two rows. @Component({ selector: '[my-row]', template: ` <tr> <td>first</td> <td>second</td> </tr> <t ...

The sequence of events in React Native following a navigation.navigate call

Seeking suggestions and advice, I currently have the following 2 lines of code within a react native expo component: this.props.navigation.navigate("App"); patchUser(this.state.dataSource.userInfo.username, this.state.dataSource.userInfo.firstN ...

The validation process in Redux forms

Imagine we have the following types defined: interface MyFormFields { FirstName: string; LastName: string; } type FieldsType = keyof MyFormFields; const field1: FieldsType = "c"; const field2 = "c" as FieldsType; Now, I am looking to implemen ...

Webpack issue arising from the latest Angular update

Each time I initiate ng serve, I encounter the following error. This was not an issue with Angular 11. Generating browser application bundles...Warning: Entry point '@ngbmodule/material-carousel' contains deep imports into 'C:/Users/Göbölà ...

Customizing the appearance of tab labels in Angular Material

I've been struggling to find a solution for changing the color of the mat-tab labels. Despite combing through numerous Stack Overflow posts on styling mat-tabs, I have not been able to change the text color from white to black. Currently, the non-acti ...

Troubleshooting: Unable to Sort Column in ngx-DataTable Angular 4

As a newcomer to Angular, I have been encountering some challenges while using ngx-DataTable. I am currently utilizing a simple ngx-DataTable for basic operations. The issue I am facing is that the sorting functionality is not working on a specific column, ...

The "rest" variable is automatically assigned the type of "any" because it lacks a specified type and is used within its own initializer

Attempting to set up a private route using react router 4 and Typescript. Check out the code I'm working with: type CustomRouteProps<T> = T & { component: any, authRequired: boolean }; function PrivateRoute({ component: Component, authRequ ...

Ways to dynamically apply styles to the component tag depending on the visibility of its content

Consider a scenario where you have a component with logic to toggle the visibility of its contents: @Component({ selector: 'hello', template: `<div *ngIf="visible"> <h1>Hello {{name}}!</h1></div>`, styles: [`h1 { fo ...

The functionality of CSS transitions may be affected when dynamically adding a class

Creating a custom CSS for my main div: .main { height: 0%; position: absolute; width: 100%; z-index: 100; background: whitesmoke; bottom: 0; border-radius: 15px; padding: 20px; color: gray; left: 0; right: 0; transition: height 1s e ...

Having trouble generating a bin executable for my npm package

Referencing: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#bin I am attempting to generate a "binary" for my npm package. The structure of my package.json is as follows: { "name": "@internal/my-exe", "version": "0.0.0", "type": "commo ...

Securely getting a data point from a pathway

Within my Angular project, I recently made the transition from using query string elements in URLs such as: http://www.whatever.com/products?productName=TheMainProduct&id=234234 To a route-based system like this: http://www.whatever.com/products/The ...

Step-by-step guide to creating a flip effect with Angular 2 animations

I've been utilizing pure css flip of cards in my project, but I feel like it's not the best solution. Does anyone know how to create a card flip in angular 2 upon clicking a button? I came across one in angularjs here <div ng-app="cardFli ...

Despite my efforts to properly run, my angular component is still not being added to the angular module

After attempting to execute ng generate component elements/ElementsHome, I encountered a successful run; however, the terminal did not display the Updated file path as a hyperlink. Instead, it indicated that the component was not created within the module. ...

Is there a way to use Jest spyOn to monitor a function that is returned by another function?

I'm curious about why the final assertion (expect(msgSpy).toBeCalled()) in this code snippet is failing. What adjustments should be made to ensure it passes? it('spyOn test', () => { const newClient = () => { const getMsg = ...

Enhance Your Search Input: Adding an Icon Before Placeholder Text in Material Design

I am currently facing an issue where the placeholder text is overlapping the icon in the code below. I need to have the placeholder text indented to the right of the icon. Is there a more efficient way to include both an icon and a placeholder text in a ...

Server-Side Rendering (SSR) is failing to generate any HTML output

I recently integrated Angular Universal SSR into my Angular application. Everything is working perfectly fine locally without any errors during the yarn build:ssr or yarn dev:ssr commands. Upon starting the server and inspecting the source, I can see all t ...

Tips on obtaining upload percentage and showcasing it on a progress bar

Hey there, I'm currently working on uploading a file to the backend and I'm looking for a way to show the upload percentage in a progress bar: Below is the code snippet for adding the file through TypeScript component: this.updateDocumentSub = t ...