数组中每个元素调用callback 函数,为等于True的元素创建一个新的数组。
array.filter(function (element, index, self) {
return true or false;
});
1.element 元素的值
2.index 元素的索引
3.self 被遍历的数组
let arr = ['AAAA', 'BBBB', 'CCCC'];
let newArr = arr.filter(function (element, index, self) {
console.log('>>>>>>>>>element>>>>>>>' + element);
console.log('>>>>>>>>>index>>>>>>>' + index);
console.log('>>>>>>>>>self>>>>>>>' + self);
return true;
});
console.log('>>>>>>>>>newArr>>>>>>>' + newArr);
使用【lightning-datatable】时,当取得所有Opportunity数据,只需要StageName = “Prospecting”的数据时
@wire(getOpportunityListView)
recordList(result) {
this.wiredRecordList = result;
if (result.data) {
this.records = result.data;
this.prospectRecords = result.data?.filter(function (element, index, self) {
return element.stageName== 'Prospecting';
});
console.log('>>>>>>>>this.records>>>>>>>' + JSON.stringify(this.records));
console.log('>>>>>>>>this.prospectRecords>>>>>>>' + JSON.stringify(this.prospectRecords));
this.error = undefined;
this.loaded = true;
} else if (result.error) {
this.error = result.error;
this.records = [];
}
}
指针函数写法
this.closedRecords = result.data?.filter(
(r) => r.stageName== 'Closed Won'
);
@wire(getOpportunityListView)
recordList(result) {
this.wiredRecordList = result;
if (result.data) {
this.records = result.data;
this.prospectRecords = result.data?.filter(function (element, index, self) {
return element.stageName== 'Prospecting';
});
this.closedRecords = result.data?.filter(
(r) => r.stageName== 'Closed Won'
);
console.log('>>>>>>>>this.records>>>>>>>' + JSON.stringify(this.records));
console.log('>>>>>>>>this.prospectRecords>>>>>>>' + JSON.stringify(this.prospectRecords));
console.log('>>>>>>>>this.closedRecords>>>>>>>' + JSON.stringify(this.closedRecords));
this.error = undefined;
this.loaded = true;
} else if (result.error) {
this.error = result.error;
this.records = [];
}
}
使用Map函数,为数组中添加新元素。
this.closedMapRecords = result.data?.map(r => {
return {...r,stageStatus: r.stageName !== 'Closed Won'}
}) ?? [];;
@wire(getOpportunityListView)
recordList(result) {
this.wiredRecordList = result;
if (result.data) {
this.records = result.data;
this.prospectRecords = result.data?.filter(function (element, index, self) {
return element.stageName== 'Prospecting';
});
this.closedRecords = result.data?.filter(
(r) => r.stageName== 'Closed Won'
);
this.closedMapRecords = result.data?.map(r => {
return {...r,stageStatus: r.stageName !== 'Closed Won'}
}) ?? [];;
console.log('>>>>>>>>this.records>>>>>>>' + JSON.stringify(this.records));
console.log('>>>>>>>>this.prospectRecords>>>>>>>' + JSON.stringify(this.prospectRecords));
console.log('>>>>>>>>this.closedRecords>>>>>>>' + JSON.stringify(this.closedRecords));
console.log('>>>>>>>>this.closedMapRecords>>>>>>>' + JSON.stringify(this.closedMapRecords));
this.error = undefined;
this.loaded = true;
} else if (result.error) {
this.error = result.error;
this.records = [];
}
}
使用concat()方法可以把两个List相结合
this.prospectRecords = result.data?.filter(function (element, index, self) {
return element.stageName== 'Prospecting';
});
this.closedRecords = result.data?.filter(
(r) => r.stageName== 'Closed Won'
);
console.log('>>>>>>>>this.prospectRecords>>>>>>>' + JSON.stringify(this.prospectRecords));
console.log('>>>>>>>>this.closedRecords>>>>>>>' + JSON.stringify(this.closedRecords));
this.records = this.prospectRecords.concat(this.closedRecords);
console.log('>>>>>>>>this.records>>>>>>>' + JSON.stringify(this.records));
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。