[英]Ionic array filter does not filter
I have a list of items in an array. 我有一个数组中的项目列表。 Each element in the array contains the following: 数组中的每个元素都包含以下内容:
id
date
status
name
description
I am trying to create a second array which will contain all the elements from the first array where the status = 'pending'. 我正在尝试创建第二个数组,其中将包含状态='pending'的第一个数组中的所有元素。
I am executing the following code in my home.ts file: 我正在home.ts文件中执行以下代码:
showPending(){
this.pendingItems = this.items;
this.pendingItems.filter((item) => {return item.status === 'pending'});
this.navCtrl.push(ShowPendingPage, {
pendingItems: this.pendingItems
});
}
When I run my application, I add 3 elements into the items array. 在运行应用程序时,我将3个元素添加到items数组中。 2 elements have a status of pending and 1 has a status of complete. 2个元素的状态为待处理,1个元素的状态为完成。 When I execute the above code, the ShowPendingPage gets pushed. 当我执行上面的代码时,ShowPendingPage被推送。 I execute this code in the ShowPendingPage.ts file: 我在ShowPendingPage.ts文件中执行以下代码:
this.passedArray = this.navParams.get('pendingItems')
and I execute the following in my ShowPendingPage.html: 然后在ShowPendingPage.html中执行以下命令:
<ion-content>
<ion-list>
<ion-item-sliding *ngFor="let item of passedArray">
<ion-item [ngClass]="item.status">
{{item.name}} - Added {{item.date}}
</ion-item>
<ion-item-options side="right">
<button ion-button color="light" (click)="viewItem(item)">View</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
The resulting array contains all 3 elements of the original array. 结果数组包含原始数组的所有3个元素。 It should only contain 2 elements, the two where I set the status to pending. 它应该只包含2个元素,其中两个元素将状态设置为“待处理”。
Anybody with better eyes than me that can see what I am missing? 有比我更好的眼睛的人可以看到我的失踪吗?
Array.prototype.filter
creates a new array with the filtered elements and is not in place . Array.prototype.filter
使用过滤后的元素创建一个新数组,并且不在原位 。
The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。
Also 也
filter() does not mutate the array on which it is called. filter()不会使调用它的数组发生变化。
You just have to set it to your variable. 您只需要将其设置为变量即可。
this.pendingItems = this.pendingItems.filter((item) => {return item.status === 'pending'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.