Filter an array of objects in Angular2 based on properties that are not included in the specified values

I'm encountering some difficulties while filtering an array of objects based on a specific set of values:

Here is the array that needs to be filtered:

const items: Product[] = ... values

Next, I have created an array containing the products that I wish to select:

const sel: Product[] = ... values

The property that I need to use for applying the filter is idProduct. How can I achieve this?

I am looking for something along the lines of:

const query = items.filter(item => sel.map(s => s.idProduct).includes(item.idProduct))

Any ideas on how to make this work?

Appreciate your assistance.

Answer №1

If you want to locate products in sel that share the same product id, you can utilize the some method.

items.filter(item => sel.some(selection => selection.idProduct == item.idProduct)); 

Answer №2

After analyzing the previous responses, I have come up with a solution using the Array.prototype.includes() method:

let result = items.filter(item => selection.map(sel => sel.productId).includes(item.productId));

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

Custom validation for Angular template-driven form fails to properly update the state of the form

I am currently working on creating a custom validator for a template-driven form. The validator I have created (shown below) works perfectly for field-level validation, but I am facing an issue where the validation state for the entire form is not updating ...

Typescript filtering function that results in an empty array

Struggling with filtering an array in typescript and aurelia as I keep getting empty lists. For example, when searching for the keyword ra within the firstName property, I expect to retrieve the object with the name "Raja". Not sure where I'm going w ...

BarChart is failing to exhibit data in tooltips when using dynamic keys

Query Description Hello! I'm currently tackling an issue with a bar chart. Everything is working smoothly, except for the default tooltip, which appears blank when hovering over the bars. My chart utilizes dynamic keys for the legends, and they are f ...

Please place the accurate image inside the designated box based on the corresponding ID number

I am currently working on a function that retrieves image data from a database and displays it in HTML using *ngFor directive. In order to display the correct image, I need to fetch the ID associated with the image data and use it to retrieve the correspo ...

What steps should I take to resolve an unhandled promise error in a React TypeScript application while making an axios POST request?

I am currently working on a .tsx file to implement adding an enterprise feature. Although I can input data, clicking the submit button does not trigger any action. My application includes a page that has a button for adding a new enterprise. Upon clickin ...

Can the script be loaded into a TypeScript file?

I'm currently in the process of integrating this script tag into my Angular 2 project, but I'm searching for a way to incorporate it into the typescript file so that I can access its methods within the .ts file. <script type="text/javascript" ...

Preventing CORS issues when making HTTP requests to an Elasticsearch server using Angular hosted on Firebase

As someone who is a bit out of touch with web technologies, I'm pretty sure this question has been asked before. However, I am struggling to understand what CORS is and why it is causing my simple HTTP request to be blocked. I am currently working on ...

In order to resolve the issue in nextjs 13 where the argument is of type 'any[] | null' and cannot be assigned to the parameter of type 'SetStateAction<never[]>', a potential solution may involve explicitly checking for null values

My current project uses Next.js 13 and is based on the Supabase database. I am attempting to fetch data from Supabase and assign it to a variable using useState, but encountering the following error: Argument of type 'any[] | null' is not assigna ...

Passing a function as a prop in a child component and invoking it in React using TypeScript

I have a function that I need to pass to a child component in order to manage the state in the parent component. The function takes an object declared in FriendListItem and adds it to an array as a new object. Despite my research efforts, I am struggling t ...

Steps to implement the click functionality on the alert controller and modify the language in Ionic 4

I am currently developing a multilingual Ionic 4 app and have implemented the alert controller to display language options. However, I am facing an issue on how to dynamically change the language based on user selection. Below is my app.component.ts code ...

How to apply animation in Angular 2 to a single element within an *ngFor loop

Is it possible to apply animation to only one element when using *ngFor in Angular? Currently, the animation applies to all elements in the cycle, but I want to target only one element. How can I achieve this? .............................................. ...

Is there a way to access the result variable outside of the lambda function?

My goal is to retrieve data from an external API using Typescript. Below is the function I have written: export class BarChartcomponent implements OnInit{ public chart: any; data: any = []; constructor(private service:PostService) { } ngOnInit( ...

Ensure that the interface limits the key value to match precisely the value of a constant in Typescript

Seeking assistance in understanding how to enforce a specific type for an optional key within an interface: const FIRST = "FIRST" const SECOND = "SECOND" interface TSomeInterface { element: Element order?: typeof FIRST | typeof ...

The Primeng Angular2 checkbox malfunctioning issue

My form setup in Angular2 CLI looks like this: Inside the component export class UsersAddComponent implements OnInit { ngOnInit() { this.userForm = this._formBuilder.group({ role: ['', [Validators.required]], others: this._for ...

Is it acceptable to manipulate the prevState parameter of the setState function as mutable?

It is commonly known that directly modifying this.state is not recommended, and instead setState should be used. Following this logic, I assumed that prevState should also be treated as immutable, and setState should always involve creating a new object i ...

Efficiently process and handle the responses from Promise.all for every API call, then save the retrieved data

Currently, I am passing three API calls to Promise.all. Each API call requires a separate error handler and data storage in its own corresponding object. If I pass test4 to Promise.all, how can I automatically generate its own error and store the data in ...

Angular 8 ngFor not displaying expected information from object

I have a set of data which includes IDs, versions, and user details for Paul and Peter. See below: var data = { id: 1 version: 1 user: [ { name: 'paul' }, { name: 'peter' ...

Angular2 charts rendered with highcharts may not adjust their height according to the parent container

Struggling to display charts using angular2-highcharts due to the chart not inheriting the parent div's height. Any suggestions on how to fix this issue? If you'd like to take a look, here is the plunker link: https://plnkr.co/edit/tk0aR3NCdKtCo ...

Breaking down the steps to flip between two images when clicked in Vue.js

I'm currently trying to implement a feature in Vue.js where images switch on click. The functionality I'm aiming for is as follows: Upon initial load, the image displays in black, then upon clicking/selecting it, the image transitions into a blu ...

Experience the mesmerizing motion of a D3.js Bar Chart as it ascends from the bottom to the top. Feel free to

Here is the snippet of code I am working with. Please check the link for the output graph demonstration. [Click here to view the output graph demo][1] (The current animation in the output is from top to bottom) I want to animate the bars from Bottom to ...