浏览记录或者购物车的去重处理
创始人
2024-05-31 20:55:16
0
saveHistory(){// 获取缓存数据let historyArr = uni.getStorageSync('historyArr') || []//需要添加的数据let item={id:this.detail.id,classid:this.detail.classid,title:this.detail.title,picurl:this.detail.picurl,looktime:parseTime(Date.now())};// forEach和findIndex的区别:forEach会循环数组,但是不会有返回值,可以在循环中做与返回值无关的数组处理,// findIndex也会循环数组,会有return处理,当return的值为true时会返回第一个为true的数据项的indexlet index = historyArr.findIndex(i=>{// 返回第一个匹配到的值的索引return i.id == this.detail.id})// 如果该索引>0,就说明数组内已经有这个浏览记录了if(index>=0){// 删除元素,第一个参数为开始处理的下标// 第二个元素为,你要删除几个元素historyArr.splice(index,1)}// 将需要添加到数据追加在缓存数据的最前面historyArr.unshift(item);// 重写写入缓存uni.setStorageSync('historyArr',historyArr)}

相关内容

热门资讯

中考英语作文好句子【优选3篇... 中考英语作文好句子 篇一The Importance of Physical ExercisePhy...
描写景色英语作文(优秀3篇) 描写景色英语作文 篇一A Tranquil Morning in the MeadowAs the ...
六一儿童节的英语作文(优秀6... 六一儿童节的英语作文 篇一Children's Day is a special day dedic...
天津首考英语作文范文【最新6... 天津首考英语作文范文 篇一The Importance of Learning EnglishEng...
写我的朋友的英语作文400字... 篇一:我的朋友My FriendI have a friend named Lily. She is...
my family 英语作文... my family 英语作文版 篇一My FamilyI come from a small but...
我的梦英语作文(通用6篇) 我的梦英语作文 篇一我一直有一个特殊的梦想,那就是成为一名优秀的英语教师。这个梦想源于我对英语的热爱...
我的朋友英语作文(精选6篇) 我的朋友英语作文 篇一My Friend LucyI would like to introduce...
志愿者的英语作文(精选6篇) 志愿者的英语作文 篇一Volunteering ExperienceI have always be...
英语作文带翻译(经典6篇) 英语作文带翻译 篇一:The Importance of Learning a Second Lan...
英语作文范文分享给朋友【实用... 英语作文范文分享给朋友 篇一:The Importance of ReadingAs an avid...
比较交友性格的好处英语作文【... 比较交友性格的好处英语作文 篇一The Benefits of Comparing Friendsh...
英语作文:Never giv... 英语作文:Never give up永不言弃 篇一Never give upPersistence ...
运动精神英语作文(推荐3篇) 运动精神英语作文 篇一:The Importance of SportsmanshipSportsm...
介绍长城的英语作文【精选4篇... Introducing the Great Wall of ChinaEssay OneThe Gr...
你会选择在家办公的工作吗英语... 你会选择在家办公的工作吗英语作文 篇一Title: Is Working from Home a G...
英语作文My hobby 英语作文My hobby(通用13篇)  在学习、工作、生活中,大家总少不了接触作文吧,借助作文人们...
世界杯英语作文【经典3篇】 世界杯英语作文 篇一Title: The Thrilling Spectacle of the Wo...
写共享单车的利与弊英语作文(... Writing 1: The Pros and Cons of Shared BicyclesSha...
中秋节的英语作文 关于中秋节的英语作文(精选6篇)  在学习、工作乃至生活中,大家都不可避免地会接触到作文吧,写作文可...