前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce JS中如何使用 Filter函数与Map函数与concat()方法

Salesforce JS中如何使用 Filter函数与Map函数与concat()方法

原创
作者头像
repick
发布2022-05-16 23:08:40
3.2K0
发布2022-05-16 23:08:40
举报
文章被收录于专栏:SalesforceSalesforce

1.Filter函数

数组中每个元素调用callback 函数,为等于True的元素创建一个新的数组。

代码语言:javascript
复制
array.filter(function (element, index, self) {
   return true or false;
});

1.element 元素的值

2.index 元素的索引

3.self 被遍历的数组

例子1

代码语言:javascript
复制
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);

例子2

使用【lightning-datatable】时,当取得所有Opportunity数据,只需要StageName = “Prospecting”的数据时

代码语言:javascript
复制
@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 = [];
        }
    }

例子3

指针函数写法

代码语言:javascript
复制
this.closedRecords = result.data?.filter(
                (r) => r.stageName== 'Closed Won'
            );
代码语言:javascript
复制
@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 = [];
        }
    }

2.Map函数

使用Map函数,为数组中添加新元素。

代码语言:javascript
复制
this.closedMapRecords = result.data?.map(r => {
  return {...r,stageStatus: r.stageName !== 'Closed Won'}
}) ?? [];;
代码语言:javascript
复制
@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 = [];
        }
    }

3.concat()方法

使用concat()方法可以把两个List相结合

代码语言:javascript
复制
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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.Filter函数
    • 例子1
    • 例子2
      • 例子3
      • 2.Map函数
      • 3.concat()方法
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档