美文网首页
按照字母排序

按照字母排序

作者: SunnyLYummy | 来源:发表于2022-06-08 15:31 被阅读0次
api_bank_list(listParams).then(result => {
            if (result && result.list.length > 0) {
                let hotList = [];
                let sectionList = [];

                //先按照字母排序
                result.list.sort((a, b) => {
                    let aFirstLetter = a.name.toLocaleUpperCase().substring(0, 1);
                    let bFirstLetter = b.name.toLocaleUpperCase().substring(0, 1);
                    return aFirstLetter.localeCompare(bFirstLetter);
                })

                let name = result.list[0].name.toLocaleUpperCase();
                let obj = {
                    title: name.substring(0, 1),
                    data: []
                }
                result.list.map((item, index) => {
                    let firstLetter = item.name.toLocaleUpperCase().substring(0, 1);
                    if (obj.title == firstLetter) {
                        obj.data.push(item)
                    } else {
                        sectionList.push(obj);
                        obj = {
                            title: firstLetter,
                            data: [item]
                        }
                    }
                    //sort>0,就是火爆  (如果是搜索就不需要筛选)
                    if (!isSearch && item.sort > 0) {
                        hotList.push(item);
                    }
                })
                this.setState({
                    sectionList,
                    hotList
                })
            }
        }).catch(error => {

        });

相关文章

网友评论

      本文标题:按照字母排序

      本文链接:https://www.haomeiwen.com/subject/wipumrtx.html