Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript filter method

In my angular 2 application I have declared 2 object arrays.

users: User[];
selectedUsers: User[];

Now I am using the filter() method to return an array with objects whose checked property is true.

this.selectedUsers = this.users.filter(user => user.isChecked == true);

The problem is the references for elements of both arrays are same. So is there a way to that I can filter out objects and return array of new objects(new reference)?

like image 265
kiranghule27 Avatar asked Aug 11 '26 00:08

kiranghule27


1 Answers

You can always use Array.prototype.map to create a new array and Object.assign to create a new object:

this.users.filter(user => user.isChecked).map(u => Object.assign({},u));
like image 199
Amir Popovich Avatar answered Aug 13 '26 14:08

Amir Popovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!